ingress-nginx-helm/controllers/gce/firewalls/fakes.go

104 lines
3.1 KiB
Go
Raw Normal View History

2016-03-08 19:32:54 +00:00
/*
Copyright 2015 The Kubernetes Authors.
2016-03-08 19:32:54 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package firewalls
import (
"fmt"
"strconv"
2016-03-08 19:32:54 +00:00
compute "google.golang.org/api/compute/v1"
netset "k8s.io/kubernetes/pkg/util/net/sets"
2017-04-01 14:38:58 +00:00
"k8s.io/ingress/controllers/gce/utils"
2016-03-08 19:32:54 +00:00
)
type fakeFirewallsProvider struct {
fw map[string]*compute.Firewall
namer *utils.Namer
2016-03-08 19:32:54 +00:00
}
// NewFakeFirewallsProvider creates a fake for firewall rules.
func NewFakeFirewallsProvider(namer *utils.Namer) *fakeFirewallsProvider {
return &fakeFirewallsProvider{
fw: make(map[string]*compute.Firewall),
namer: namer,
2016-03-08 19:32:54 +00:00
}
}
func (f *fakeFirewallsProvider) GetFirewall(prefixedName string) (*compute.Firewall, error) {
rule, exists := f.fw[prefixedName]
if exists {
return rule, nil
}
return nil, utils.FakeGoogleAPINotFoundErr()
}
func (f *fakeFirewallsProvider) CreateFirewall(name, msgTag string, srcRange netset.IPNet, ports []int64, hosts []string) error {
prefixedName := f.namer.FrName(name)
2016-03-08 19:32:54 +00:00
strPorts := []string{}
for _, p := range ports {
strPorts = append(strPorts, strconv.FormatInt(p, 10))
}
if _, exists := f.fw[prefixedName]; exists {
return fmt.Errorf("firewall rule %v already exists", prefixedName)
2016-03-08 19:32:54 +00:00
}
f.fw[prefixedName] = &compute.Firewall{
2016-03-08 19:32:54 +00:00
// To accurately mimic the cloudprovider we need to add the k8s-fw
// prefix to the given rule name.
Name: prefixedName,
2016-03-08 19:32:54 +00:00
SourceRanges: srcRange.StringSlice(),
2016-04-18 01:05:49 +00:00
Allowed: []*compute.FirewallAllowed{{Ports: strPorts}},
TargetTags: hosts, // WARNING: This is actually not correct, but good enough for testing this package
}
2016-03-08 19:32:54 +00:00
return nil
}
func (f *fakeFirewallsProvider) DeleteFirewall(name string) error {
2016-03-08 19:32:54 +00:00
// We need the full name for the same reason as CreateFirewall.
prefixedName := f.namer.FrName(name)
_, exists := f.fw[prefixedName]
2016-03-08 19:32:54 +00:00
if !exists {
return utils.FakeGoogleAPINotFoundErr()
2016-03-08 19:32:54 +00:00
}
delete(f.fw, prefixedName)
2016-03-08 19:32:54 +00:00
return nil
}
func (f *fakeFirewallsProvider) UpdateFirewall(name, msgTag string, srcRange netset.IPNet, ports []int64, hosts []string) error {
2016-03-08 19:32:54 +00:00
strPorts := []string{}
for _, p := range ports {
strPorts = append(strPorts, strconv.FormatInt(p, 10))
2016-03-08 19:32:54 +00:00
}
// We need the full name for the same reason as CreateFirewall.
prefixedName := f.namer.FrName(name)
_, exists := f.fw[prefixedName]
if !exists {
return fmt.Errorf("update failed for rule %v, srcRange %v ports %v, rule not found", prefixedName, srcRange, ports)
2016-03-08 19:32:54 +00:00
}
f.fw[prefixedName] = &compute.Firewall{
Name: name,
SourceRanges: srcRange.StringSlice(),
Allowed: []*compute.FirewallAllowed{{Ports: strPorts}},
TargetTags: hosts, // WARNING: This is actually not correct, but good enough for testing this package
}
return nil
2016-03-08 19:32:54 +00:00
}