diff --git a/pkg/loadbalancers/l7.go b/pkg/loadbalancers/l7.go index 488c9c0947..af5e1ee045 100644 --- a/pkg/loadbalancers/l7.go +++ b/pkg/loadbalancers/l7.go @@ -125,7 +125,7 @@ type L7 struct { // Warning: This should be used only for logging and should not be used to // retrieve/ delete gce resource names. func (l *L7) String() string { - return l.namer.LbName() + return l.namer.LoadBalancer().String() } // Versions returns the struct listing the versions for every resource diff --git a/pkg/loadbalancers/l7s.go b/pkg/loadbalancers/l7s.go index 6d80511085..481d499575 100644 --- a/pkg/loadbalancers/l7s.go +++ b/pkg/loadbalancers/l7s.go @@ -22,7 +22,6 @@ import ( "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" "k8s.io/api/networking/v1beta1" - "k8s.io/apimachinery/pkg/util/sets" "k8s.io/ingress-gce/pkg/common/operator" "k8s.io/ingress-gce/pkg/composite" "k8s.io/ingress-gce/pkg/events" @@ -124,9 +123,9 @@ func (l *L7s) GCv2(ing *v1beta1.Ingress) error { func (l *L7s) GCv1(names []string) error { klog.V(2).Infof("GCv1(%v)", names) - knownLoadBalancers := sets.NewString() + knownLoadBalancers := make(map[namer_util.LoadBalancerName]bool) for _, n := range names { - knownLoadBalancers.Insert(l.v1NamerHelper.LoadBalancer(n)) + knownLoadBalancers[l.v1NamerHelper.LoadBalancer(n)] = true } // GC L7-ILB LBs if enabled @@ -160,15 +159,14 @@ func (l *L7s) GCv1(names []string) error { // gc is a helper for GCv1. // TODO(shance): get versions from description -func (l *L7s) gc(urlMaps []*composite.UrlMap, knownLoadBalancers sets.String, versions *features.ResourceVersions) []error { +func (l *L7s) gc(urlMaps []*composite.UrlMap, knownLoadBalancers map[namer_util.LoadBalancerName]bool, versions *features.ResourceVersions) []error { var errors []error // Delete unknown loadbalancers for _, um := range urlMaps { - nameParts := l.v1NamerHelper.ParseName(um.Name) - l7Name := l.v1NamerHelper.LoadBalancerFromLbName(nameParts.LbName) + l7Name := l.v1NamerHelper.LoadBalancerForURLMap(um.Name) - if knownLoadBalancers.Has(l7Name) { + if knownLoadBalancers[l7Name] { klog.V(3).Infof("Load balancer %v is still valid, not GC'ing", l7Name) continue } @@ -179,7 +177,7 @@ func (l *L7s) gc(urlMaps []*composite.UrlMap, knownLoadBalancers sets.String, ve continue } - if err := l.delete(l.namerFactory.NamerForLbName(l7Name), versions, scope); err != nil { + if err := l.delete(l.namerFactory.NamerForLoadBalancer(l7Name), versions, scope); err != nil { errors = append(errors, fmt.Errorf("error deleting loadbalancer %q", l7Name)) } } diff --git a/pkg/loadbalancers/l7s_test.go b/pkg/loadbalancers/l7s_test.go index 9080d5221d..f9d7da8892 100644 --- a/pkg/loadbalancers/l7s_test.go +++ b/pkg/loadbalancers/l7s_test.go @@ -179,13 +179,13 @@ func TestGC(t *testing.T) { versions := features.GAResourceVersions for _, key := range otherKeys { - namer := otherFeNamerFactory.NamerForLbName(otherNamer.LoadBalancer(key)) + namer := otherFeNamerFactory.NamerForLoadBalancer(otherNamer.LoadBalancer(key)) createFakeLoadbalancer(cloud, namer, versions, defaultScope) } for _, tc := range testCases { for _, key := range tc.gcpLBs { - namer := namerFactory.NamerForLbName(v1NamerHelper.LoadBalancer(key)) + namer := namerFactory.NamerForLoadBalancer(v1NamerHelper.LoadBalancer(key)) createFakeLoadbalancer(cloud, namer, versions, defaultScope) } @@ -196,7 +196,7 @@ func TestGC(t *testing.T) { // check if other LB are not deleted for _, key := range otherKeys { - namer := otherFeNamerFactory.NamerForLbName(otherNamer.LoadBalancer(key)) + namer := otherFeNamerFactory.NamerForLoadBalancer(otherNamer.LoadBalancer(key)) if err := checkFakeLoadBalancer(cloud, namer, versions, defaultScope, true); err != nil { t.Errorf("For case %q and key %q, do not expect err: %v", tc.desc, key, err) } @@ -211,7 +211,7 @@ func TestGC(t *testing.T) { // check if the ones that are expected to be GC is actually GCed. expectRemovedLBs := sets.NewString(tc.gcpLBs...).Difference(sets.NewString(tc.expectLBs...)).Difference(sets.NewString(tc.ingressLBs...)) for _, key := range expectRemovedLBs.List() { - namer := namerFactory.NamerForLbName(v1NamerHelper.LoadBalancer(key)) + namer := namerFactory.NamerForLoadBalancer(v1NamerHelper.LoadBalancer(key)) if err := checkFakeLoadBalancer(cloud, namer, versions, defaultScope, false); err != nil { t.Errorf("For case %q and key %q, do not expect err: %v", tc.desc, key, err) } @@ -219,7 +219,7 @@ func TestGC(t *testing.T) { // check if all expected LBs exists for _, key := range tc.expectLBs { - namer := namerFactory.NamerForLbName(v1NamerHelper.LoadBalancer(key)) + namer := namerFactory.NamerForLoadBalancer(v1NamerHelper.LoadBalancer(key)) if err := checkFakeLoadBalancer(cloud, namer, versions, defaultScope, true); err != nil { t.Errorf("For case %q and key %q, do not expect err: %v", tc.desc, key, err) } @@ -249,7 +249,7 @@ func TestDoNotGCWantedLB(t *testing.T) { versions := features.GAResourceVersions for _, tc := range testCases { - namer := l7sPool.namerFactory.NamerForLbName(l7sPool.v1NamerHelper.LoadBalancer(tc.key)) + namer := l7sPool.namerFactory.NamerForLoadBalancer(l7sPool.v1NamerHelper.LoadBalancer(tc.key)) createFakeLoadbalancer(l7sPool.cloud, namer, versions, defaultScope) err := l7sPool.GCv1([]string{tc.key}) if err != nil { @@ -285,7 +285,7 @@ func TestGCToLeakLB(t *testing.T) { versions := features.GAResourceVersions for _, tc := range testCases { - namer := l7sPool.namerFactory.NamerForLbName(l7sPool.v1NamerHelper.LoadBalancer(tc.key)) + namer := l7sPool.namerFactory.NamerForLoadBalancer(l7sPool.v1NamerHelper.LoadBalancer(tc.key)) createFakeLoadbalancer(l7sPool.cloud, namer, versions, defaultScope) err := l7sPool.GCv1([]string{}) if err != nil { diff --git a/pkg/loadbalancers/loadbalancers_test.go b/pkg/loadbalancers/loadbalancers_test.go index 32e5971e00..a3f557800a 100644 --- a/pkg/loadbalancers/loadbalancers_test.go +++ b/pkg/loadbalancers/loadbalancers_test.go @@ -343,7 +343,7 @@ func TestCertUpdate(t *testing.T) { } // Verify certs - t.Logf("lbName=%q, name=%q", feNamer.LbName(), certName1) + t.Logf("lbName=%q, name=%q", feNamer.LoadBalancer(), certName1) expectCerts := map[string]string{certName1: lbInfo.TLS[0].Cert} verifyCertAndProxyLink(expectCerts, expectCerts, j, t) @@ -508,7 +508,7 @@ func TestUpgradeToNewCertNames(t *testing.T) { UrlMap: gceUrlMap, Ingress: ing, } - oldCertName := "k8s-ssl-" + feNamer.LbName() + oldCertName := fmt.Sprintf("k8s-ssl-%s", feNamer.LoadBalancer()) tlsCert := createCert("key", "cert", "name") lbInfo.TLS = []*TLSCerts{tlsCert} newCertName := feNamer.SSLCertName(tlsCert.CertHash) @@ -1123,7 +1123,7 @@ func TestClusterNameChange(t *testing.T) { // Now the components should get renamed with the next suffix. l7, err = j.pool.Ensure(lbInfo) - if err != nil || j.namer.ParseName(l7.namer.LbName()).ClusterName != newName { + if err != nil || j.namer.ParseName(l7.namer.LoadBalancer().String()).ClusterName != newName { t.Fatalf("Expected L7 name to change.") } verifyHTTPSForwardingRuleAndProxyLinks(t, j, l7) diff --git a/pkg/loadbalancers/url_maps_test.go b/pkg/loadbalancers/url_maps_test.go index 8843d14b4b..76dcee06e3 100644 --- a/pkg/loadbalancers/url_maps_test.go +++ b/pkg/loadbalancers/url_maps_test.go @@ -82,7 +82,7 @@ func TestToComputeURLMap(t *testing.T) { } namerFactory := namer_util.NewFrontendNamerFactory(namer, "") - feNamer := namerFactory.NamerForLbName("ns/lb-name") + feNamer := namerFactory.NamerForLoadBalancer("ns/lb-name") gotComputeURLMap := toCompositeURLMap(gceURLMap, feNamer, meta.GlobalKey("ns-lb-name")) if !mapsEqual(gotComputeURLMap, wantComputeMap) { t.Errorf("toComputeURLMap() = \n%+v\n want\n%+v", gotComputeURLMap, wantComputeMap) diff --git a/pkg/utils/namer/frontendnamer.go b/pkg/utils/namer/frontendnamer.go index 513750e111..7890458d10 100644 --- a/pkg/utils/namer/frontendnamer.go +++ b/pkg/utils/namer/frontendnamer.go @@ -59,7 +59,7 @@ type Scheme string type V1IngressFrontendNamer struct { ing *v1beta1.Ingress namer *Namer - lbName string + lbName LoadBalancerName } // newV1IngressFrontendNamer returns v1 frontend namer for given ingress. @@ -68,8 +68,8 @@ func newV1IngressFrontendNamer(ing *v1beta1.Ingress, namer *Namer) IngressFronte return &V1IngressFrontendNamer{ing: ing, namer: namer, lbName: lbName} } -// newV1IngressFrontendNamerFromLBName returns v1 frontend namer for load balancer. -func newV1IngressFrontendNamerFromLBName(lbName string, namer *Namer) IngressFrontendNamer { +// newV1IngressFrontendNamerForLoadBalancer returns v1 frontend namer for load balancer. +func newV1IngressFrontendNamerForLoadBalancer(lbName LoadBalancerName, namer *Namer) IngressFrontendNamer { return &V1IngressFrontendNamer{namer: namer, lbName: lbName} } @@ -103,8 +103,8 @@ func (ln *V1IngressFrontendNamer) IsLegacySSLCert(certName string) bool { return ln.namer.IsLegacySSLCert(ln.lbName, certName) } -// LbName implements IngressFrontendNamer. -func (ln *V1IngressFrontendNamer) LbName() string { +// LoadBalancer implements IngressFrontendNamer. +func (ln *V1IngressFrontendNamer) LoadBalancer() LoadBalancerName { return ln.lbName } @@ -114,7 +114,7 @@ type V2IngressFrontendNamer struct { // prefix for all resource names (ex.: "k8s"). prefix string // Load balancer name to be included in resource name. - lbName string + lbName LoadBalancerName // clusterUID is an 8 character hash to be included in resource names. // This is immutable after the cluster is created. Kube-system uid which is // immutable is used as cluster UID for v2 naming scheme. @@ -135,12 +135,12 @@ type V2IngressFrontendNamer struct { func newV2IngressFrontendNamer(ing *v1beta1.Ingress, kubeSystemUID string, prefix string) IngressFrontendNamer { clusterUID := common.ContentHash(kubeSystemUID, clusterUIDLength) namer := &V2IngressFrontendNamer{ing: ing, prefix: prefix, clusterUID: clusterUID} - // Initialize LbName. + // Initialize lbName. truncFields := TrimFieldsEvenly(maximumAllowedCombinedLength, ing.Namespace, ing.Name) truncNamespace := truncFields[0] truncName := truncFields[1] suffix := namer.suffix(kubeSystemUID, ing.Namespace, ing.Name) - namer.lbName = fmt.Sprintf("%s-%s-%s-%s", clusterUID, truncNamespace, truncName, suffix) + namer.lbName = LoadBalancerName(fmt.Sprintf("%s-%s-%s-%s", clusterUID, truncNamespace, truncName, suffix)) return namer } @@ -192,9 +192,9 @@ func (vn *V2IngressFrontendNamer) IsLegacySSLCert(certName string) bool { return false } -// LbName returns loadbalancer name. +// LoadBalancer returns loadbalancer name. // Note that this is used for generating GCE resource names. -func (vn *V2IngressFrontendNamer) LbName() string { +func (vn *V2IngressFrontendNamer) LoadBalancer() LoadBalancerName { return vn.lbName } @@ -207,7 +207,7 @@ func (vn *V2IngressFrontendNamer) suffix(uid, namespace, name string) string { // lbNameToHash returns hash string of length 16 of lbName. func (vn *V2IngressFrontendNamer) lbNameToHash() string { - return common.ContentHash(vn.lbName, 16) + return common.ContentHash(vn.lbName.String(), 16) } // FrontendNamerFactory implements IngressFrontendNamerFactory. @@ -237,7 +237,7 @@ func (rn *FrontendNamerFactory) Namer(ing *v1beta1.Ingress) IngressFrontendNamer } } -// NamerForLbName implements IngressFrontendNamerFactory. -func (rn *FrontendNamerFactory) NamerForLbName(lbName string) IngressFrontendNamer { - return newV1IngressFrontendNamerFromLBName(lbName, rn.namer) +// NamerForLoadBalancer implements IngressFrontendNamerFactory. +func (rn *FrontendNamerFactory) NamerForLoadBalancer(lbName LoadBalancerName) IngressFrontendNamer { + return newV1IngressFrontendNamerForLoadBalancer(lbName, rn.namer) } diff --git a/pkg/utils/namer/frontendnamer_test.go b/pkg/utils/namer/frontendnamer_test.go index ea5232176e..32f90c80a8 100644 --- a/pkg/utils/namer/frontendnamer_test.go +++ b/pkg/utils/namer/frontendnamer_test.go @@ -51,7 +51,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { namespace string name string // Expected values. - lbName string + lbName LoadBalancerName targetHTTPProxy string targetHTTPSProxy string sslCert string @@ -63,7 +63,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { "simple case", "namespace", "name", - "namespace-name--uid1", + LoadBalancerName("namespace-name--uid1"), "%s-tp-namespace-name--uid1", "%s-tps-namespace-name--uid1", "%s-ssl-9a60a5272f6eee97-%s--uid1", @@ -76,7 +76,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { // Total combined length of namespace and name is 47. longString[:23], longString[:24], - "01234567890123456789012-012345678901234567890123--uid1", + LoadBalancerName("01234567890123456789012-012345678901234567890123--uid1"), "%s-tp-01234567890123456789012-012345678901234567890123--uid1", "%s-tps-01234567890123456789012-012345678901234567890123--uid1", "%s-ssl-4169c63684f5e4cd-%s--uid1", @@ -89,7 +89,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { // Total combined length of namespace and name is 48. longString[:24], longString[:24], - "012345678901234567890123-012345678901234567890123--uid1", + LoadBalancerName("012345678901234567890123-012345678901234567890123--uid1"), "%s-tp-012345678901234567890123-012345678901234567890123--uid1", "%s-tps-012345678901234567890123-012345678901234567890123--uid0", "%s-ssl-c7616cb0f76c2df2-%s--uid1", @@ -102,7 +102,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { // Total combined length of namespace and name is 49. longString[:24], longString[:25], - "012345678901234567890123-0123456789012345678901234--uid1", + LoadBalancerName("012345678901234567890123-0123456789012345678901234--uid1"), "%s-tp-012345678901234567890123-0123456789012345678901234--uid0", "%s-tps-012345678901234567890123-0123456789012345678901234--ui0", "%s-ssl-537beba3a874a029-%s--uid1", @@ -114,7 +114,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { "long namespace", longString, "0", - "01234567890123456789012345678901234567890123456789-0--uid1", + LoadBalancerName("01234567890123456789012345678901234567890123456789-0--uid1"), "%s-tp-01234567890123456789012345678901234567890123456789-0--u0", "%s-tps-01234567890123456789012345678901234567890123456789-0--0", "%s-ssl-92bdb5e4d378b3ce-%s--uid1", @@ -126,7 +126,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { "long name", "0", longString, - "0-01234567890123456789012345678901234567890123456789--uid1", + LoadBalancerName("0-01234567890123456789012345678901234567890123456789--uid1"), "%s-tp-0-01234567890123456789012345678901234567890123456789--u0", "%s-tps-0-01234567890123456789012345678901234567890123456789--0", "%s-ssl-8f3d42933afb5d1c-%s--uid1", @@ -138,7 +138,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { "long name and namespace", longString, longString, - "01234567890123456789012345678901234567890123456789-012345678900", + LoadBalancerName("01234567890123456789012345678901234567890123456789-012345678900"), "%s-tp-01234567890123456789012345678901234567890123456789-01230", "%s-tps-01234567890123456789012345678901234567890123456789-0120", "%s-ssl-a04f7492b36aeb20-%s--uid1", @@ -155,7 +155,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { t.Run(tc.desc, func(t *testing.T) { key := fmt.Sprintf("%s/%s", tc.namespace, tc.name) t.Logf("Ingress key %s", key) - namer := newV1IngressFrontendNamerFromLBName(oldNamer.LoadBalancer(key), oldNamer) + namer := newV1IngressFrontendNamerForLoadBalancer(oldNamer.LoadBalancer(key), oldNamer) tc.targetHTTPProxy = fmt.Sprintf(tc.targetHTTPProxy, prefix) tc.targetHTTPSProxy = fmt.Sprintf(tc.targetHTTPSProxy, prefix) tc.sslCert = fmt.Sprintf(tc.sslCert, prefix, secretHash) @@ -164,8 +164,8 @@ func TestV1IngressFrontendNamer(t *testing.T) { tc.urlMap = fmt.Sprintf(tc.urlMap, prefix) // Test behavior of V1 Namer created using load-balancer name. - if diff := cmp.Diff(tc.lbName, namer.LbName()); diff != "" { - t.Errorf("namer.LbName() mismatch (-want +got):\n%s", diff) + if diff := cmp.Diff(tc.lbName, namer.LoadBalancer()); diff != "" { + t.Errorf("namer.LoadBalancer() mismatch (-want +got):\n%s", diff) } targetHTTPProxyName := namer.TargetProxy(HTTPProtocol) if diff := cmp.Diff(tc.targetHTTPProxy, targetHTTPProxyName); diff != "" { @@ -194,7 +194,7 @@ func TestV1IngressFrontendNamer(t *testing.T) { // Ensure that V1 Namer returns same values as old namer. lbName := oldNamer.LoadBalancer(key) - if diff := cmp.Diff(lbName, namer.LbName()); diff != "" { + if diff := cmp.Diff(lbName, namer.LoadBalancer()); diff != "" { t.Errorf("Got diff between old and V1 namers, lbName mismatch (-want +got):\n%s", diff) } if diff := cmp.Diff(oldNamer.TargetProxy(lbName, HTTPProtocol), targetHTTPProxyName); diff != "" { @@ -249,7 +249,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { namespace string name string // Expected values. - lbName string + lbName LoadBalancerName targetHTTPProxy string targetHTTPSProxy string sslCert string @@ -273,7 +273,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "62 characters", longString[:23], longString[:24], - "7kpbhpki-012345678901234567-012345678901234567-hg17g9tx", + LoadBalancerName("7kpbhpki-012345678901234567-012345678901234567-hg17g9tx"), "%s2-tp-7kpbhpki-012345678901234567-012345678901234567-hg17g9tx", "%s2-ts-7kpbhpki-012345678901234567-012345678901234567-hg17g9tx", "%s2-cr-7kpbhpki-ktiggo5yie4uh72b-%s", @@ -285,7 +285,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "63 characters", longString[:24], longString[:24], - "7kpbhpki-012345678901234567-012345678901234567-o0dahbae", + LoadBalancerName("7kpbhpki-012345678901234567-012345678901234567-o0dahbae"), "%s2-tp-7kpbhpki-012345678901234567-012345678901234567-o0dahbae", "%s2-ts-7kpbhpki-012345678901234567-012345678901234567-o0dahbae", "%s2-cr-7kpbhpki-kk38dnbt6k8zrg76-%s", @@ -297,7 +297,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "64 characters", longString[:24], longString[:25], - "7kpbhpki-012345678901234567-012345678901234567-sxo4pxda", + LoadBalancerName("7kpbhpki-012345678901234567-012345678901234567-sxo4pxda"), "%s2-tp-7kpbhpki-012345678901234567-012345678901234567-sxo4pxda", "%s2-ts-7kpbhpki-012345678901234567-012345678901234567-sxo4pxda", "%s2-cr-7kpbhpki-n2b7ixc007o1ddma-%s", @@ -309,7 +309,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "long namespace", longString, "0", - "7kpbhpki-012345678901234567890123456789012345--v8ajgbg3", + LoadBalancerName("7kpbhpki-012345678901234567890123456789012345--v8ajgbg3"), "%s2-tp-7kpbhpki-012345678901234567890123456789012345--v8ajgbg3", "%s2-ts-7kpbhpki-012345678901234567890123456789012345--v8ajgbg3", "%s2-cr-7kpbhpki-m6a592dazogk94ra-%s", @@ -321,7 +321,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "long name", "0", longString, - "7kpbhpki-0-01234567890123456789012345678901234-fyhus2f6", + LoadBalancerName("7kpbhpki-0-01234567890123456789012345678901234-fyhus2f6"), "%s2-tp-7kpbhpki-0-01234567890123456789012345678901234-fyhus2f6", "%s2-ts-7kpbhpki-0-01234567890123456789012345678901234-fyhus2f6", "%s2-cr-7kpbhpki-a33x986k79kbu0me-%s", @@ -333,7 +333,7 @@ func TestV2IngressFrontendNamer(t *testing.T) { "long name and namespace", longString, longString, - "7kpbhpki-012345678901234567-012345678901234567-69z4wrm0", + LoadBalancerName("7kpbhpki-012345678901234567-012345678901234567-69z4wrm0"), "%s2-tp-7kpbhpki-012345678901234567-012345678901234567-69z4wrm0", "%s2-ts-7kpbhpki-012345678901234567-012345678901234567-69z4wrm0", "%s2-cr-7kpbhpki-5pu4c55s4c47rr9e-%s", @@ -358,8 +358,8 @@ func TestV2IngressFrontendNamer(t *testing.T) { tc.urlMap = fmt.Sprintf(tc.urlMap, prefix) // Test behavior of v2 Namer. - if diff := cmp.Diff(tc.lbName, namer.LbName()); diff != "" { - t.Errorf("namer.GetLbName() mismatch (-want +got):\n%s", diff) + if diff := cmp.Diff(tc.lbName, namer.LoadBalancer()); diff != "" { + t.Errorf("namer.LoadBalancer() mismatch (-want +got):\n%s", diff) } name := namer.TargetProxy(HTTPProtocol) if diff := cmp.Diff(tc.targetHTTPProxy, name); diff != "" { diff --git a/pkg/utils/namer/interfaces.go b/pkg/utils/namer/interfaces.go index 82f569b935..41160b6d09 100644 --- a/pkg/utils/namer/interfaces.go +++ b/pkg/utils/namer/interfaces.go @@ -33,8 +33,8 @@ type IngressFrontendNamer interface { // and cert is managed by this ingress. // old naming convention is of the form k8s-ssl- or k8s-ssl-1-. IsLegacySSLCert(certName string) bool - // LbName returns load-balancer name for the ingress. - LbName() string + // LoadBalancer returns load-balancer name for the ingress. + LoadBalancer() LoadBalancerName } // IngressFrontendNamerFactory is an interface to create a front namer for an Ingress @@ -42,9 +42,9 @@ type IngressFrontendNamer interface { type IngressFrontendNamerFactory interface { // Namer returns IngressFrontendNamer for given ingress. Namer(ing *v1beta1.Ingress) IngressFrontendNamer - // NamerForLbName returns IngressFrontendNamer given a load-balancer + // NamerForLoadBalancer returns IngressFrontendNamer given a load-balancer // name. This used only for v1 naming scheme. - NamerForLbName(lbName string) IngressFrontendNamer + NamerForLoadBalancer(loadBalancer LoadBalancerName) IngressFrontendNamer } // BackendNamer is an interface to name GCE backend resources. It wraps backend @@ -67,12 +67,9 @@ type BackendNamer interface { // V1FrontendNamer wraps frontend naming policy helper functions of namer.Namer. type V1FrontendNamer interface { // LoadBalancer constructs a loadbalancer name from the given ingress key. - LoadBalancer(ingKey string) string - // LoadBalancerFromLbName reconstructs the full loadbalancer name, given the - // lbName portion from NameComponents. - LoadBalancerFromLbName(lbName string) string - // ParseName parses the resource name of a resource generated by the namer. - ParseName(resourceName string) *NameComponents + LoadBalancer(ingKey string) LoadBalancerName + // LoadBalancerForURLMap returns the loadbalancer name for given URL map. + LoadBalancerForURLMap(urlMap string) LoadBalancerName // NameBelongsToCluster checks if a given frontend resource name is tagged with // this cluster's UID. NameBelongsToCluster(resourceName string) bool diff --git a/pkg/utils/namer/namer.go b/pkg/utils/namer/namer.go index 32d529716d..449c86219c 100644 --- a/pkg/utils/namer/namer.go +++ b/pkg/utils/namer/namer.go @@ -87,6 +87,13 @@ const ( // parameters to Namer. type NamerProtocol string +// LoadBalancerName is the name of a GCE load-balancer for an ingress. +type LoadBalancerName string + +func (lbName LoadBalancerName) String() string { + return string(lbName) +} + const ( HTTPProtocol NamerProtocol = "HTTP" HTTPSProtocol NamerProtocol = "HTTPS" @@ -121,9 +128,9 @@ func NewNamerWithPrefix(prefix, clusterName, firewallName string) *Namer { // NameComponents is a struct representing the components of a a GCE // resource name constructed by the namer. The format of such a name // is: k8s-resource---uid -// Note that the LbName field is empty if the resource is a BackendService. +// Note that the LbNamePrefix field is empty if the resource is a BackendService. type NameComponents struct { - ClusterName, Resource, Metadata, LbName string + ClusterName, Resource, Metadata, LbNamePrefix string } // SetUID sets the UID/name of this cluster. @@ -211,14 +218,14 @@ func (n *Namer) decorateName(name string) string { // Backend, InstanceGroup, UrlMap. func (n *Namer) ParseName(name string) *NameComponents { l := strings.Split(name, clusterNameDelimiter) - var uid, resource, lbName string + var uid, resource, lbNamePrefix string if len(l) >= 2 { uid = l[len(l)-1] } // We want to split the remainder of the name, minus the cluster-delimited // portion. This should resemble: - // UID-resource-loadbalancername + // prefix-resource-loadbalancernameprefix c := strings.Split(l[0], "-") if len(c) >= 2 { resource = c[1] @@ -231,13 +238,13 @@ func (n *Namer) ParseName(name string) *NameComponents { if resource == urlMapPrefix { // It is possible for the loadbalancer name to have dashes in it - so we // join the remaining name parts. - lbName = strings.Join(c[2:], "-") + lbNamePrefix = strings.Join(c[2:], "-") } return &NameComponents{ - ClusterName: uid, - Resource: resource, - LbName: lbName, + ClusterName: uid, + Resource: resource, + LbNamePrefix: lbNamePrefix, } } @@ -326,7 +333,7 @@ func (n *Namer) FirewallRule() string { // LoadBalancer constructs a loadbalancer name from the given key. The key // is usually the namespace/name of a Kubernetes Ingress. -func (n *Namer) LoadBalancer(key string) string { +func (n *Namer) LoadBalancer(key string) LoadBalancerName { // TODO: Pipe the clusterName through, for now it saves code churn // to just grab it globally, especially since we haven't decided how // to handle namespace conflicts in the Ubernetes context. @@ -334,20 +341,19 @@ func (n *Namer) LoadBalancer(key string) string { scrubbedName := strings.Replace(key, "/", "-", -1) clusterName := n.UID() if clusterName == "" || parts[len(parts)-1] == clusterName { - return scrubbedName + return LoadBalancerName(scrubbedName) } - return truncate(fmt.Sprintf("%v%v%v", scrubbedName, clusterNameDelimiter, clusterName)) + return LoadBalancerName(truncate(fmt.Sprintf("%v%v%v", scrubbedName, clusterNameDelimiter, clusterName))) } -// LoadBalancerFromLbName reconstructs the full loadbalancer name, given the -// lbName portion from NameComponents -func (n *Namer) LoadBalancerFromLbName(lbName string) string { - return truncate(fmt.Sprintf("%v%v%v", lbName, clusterNameDelimiter, n.UID())) +// LoadBalancerForURLMap returns the loadbalancer name for given URL map. +func (n *Namer) LoadBalancerForURLMap(urlMap string) LoadBalancerName { + return LoadBalancerName(truncate(fmt.Sprintf("%v%v%v", n.ParseName(urlMap).LbNamePrefix, clusterNameDelimiter, n.UID()))) } // TargetProxy returns the name for target proxy given the load // balancer name and the protocol. -func (n *Namer) TargetProxy(lbName string, protocol NamerProtocol) string { +func (n *Namer) TargetProxy(lbName LoadBalancerName, protocol NamerProtocol) string { switch protocol { case HTTPProtocol: return truncate(fmt.Sprintf("%v-%v-%v", n.prefix, targetHTTPProxyPrefix, lbName)) @@ -360,20 +366,20 @@ func (n *Namer) TargetProxy(lbName string, protocol NamerProtocol) string { // IsCertUsedForLB returns true if the resourceName belongs to this cluster's ingress. // It checks that the hashed lbName exists and -func (n *Namer) IsCertUsedForLB(lbName, resourceName string) bool { +func (n *Namer) IsCertUsedForLB(lbName LoadBalancerName, resourceName string) bool { lbNameHash := n.lbNameToHash(lbName) prefix := fmt.Sprintf("%s-%s-%s", n.prefix, sslCertPrefix, lbNameHash) return strings.HasPrefix(resourceName, prefix) && strings.HasSuffix(resourceName, n.UID()) } -func (n *Namer) lbNameToHash(lbName string) string { +func (n *Namer) lbNameToHash(lbName LoadBalancerName) string { ingHash := fmt.Sprintf("%x", sha256.Sum256([]byte(lbName))) return ingHash[:16] } // IsLegacySSLCert returns true if certName is an Ingress managed name following the older naming convention. The check // also ensures that the cert is managed by the specific ingress instance - lbName -func (n *Namer) IsLegacySSLCert(lbName string, resourceName string) bool { +func (n *Namer) IsLegacySSLCert(lbName LoadBalancerName, resourceName string) bool { // old style name is of the form k8s-ssl- or k8s-ssl-1-. legacyPrefixPrimary := truncate(fmt.Sprintf("%s-%s-%s", n.prefix, sslCertPrefix, lbName)) legacyPrefixSec := truncate(fmt.Sprintf("%s-%s-1-%s", n.prefix, sslCertPrefix, lbName)) @@ -381,14 +387,14 @@ func (n *Namer) IsLegacySSLCert(lbName string, resourceName string) bool { } // SSLCertName returns the name of the certificate. -func (n *Namer) SSLCertName(lbName string, secretHash string) string { +func (n *Namer) SSLCertName(lbName LoadBalancerName, secretHash string) string { lbNameHash := n.lbNameToHash(lbName) // k8s-ssl-[lbNameHash]-[certhash]--[clusterUID] return n.decorateName(fmt.Sprintf("%s-%s-%s-%s", n.prefix, sslCertPrefix, lbNameHash, secretHash)) } // ForwardingRule returns the name of the forwarding rule prefix. -func (n *Namer) ForwardingRule(lbName string, protocol NamerProtocol) string { +func (n *Namer) ForwardingRule(lbName LoadBalancerName, protocol NamerProtocol) string { switch protocol { case HTTPProtocol: return truncate(fmt.Sprintf("%v-%v-%v", n.prefix, forwardingRulePrefix, lbName)) @@ -400,7 +406,7 @@ func (n *Namer) ForwardingRule(lbName string, protocol NamerProtocol) string { } // UrlMap returns the name for the UrlMap for a given load balancer. -func (n *Namer) UrlMap(lbName string) string { +func (n *Namer) UrlMap(lbName LoadBalancerName) string { return truncate(fmt.Sprintf("%v-%v-%v", n.prefix, urlMapPrefix, lbName)) } diff --git a/pkg/utils/namer/namer_test.go b/pkg/utils/namer/namer_test.go index 996aae434d..016f0f68db 100644 --- a/pkg/utils/namer/namer_test.go +++ b/pkg/utils/namer/namer_test.go @@ -109,7 +109,7 @@ func TestNamerParseName(t *testing.T) { {newNamer.SSLCertName("default/my-ing", secretHash), &NameComponents{ClusterName: uid, Resource: "ssl"}}, {newNamer.ForwardingRule(lbName, HTTPProtocol), &NameComponents{ClusterName: uid, Resource: "fw"}}, {newNamer.ForwardingRule(lbName, HTTPSProtocol), &NameComponents{ClusterName: uid, Resource: "fws"}}, - {newNamer.UrlMap(lbName), &NameComponents{ClusterName: uid, Resource: "um", LbName: "key1"}}, + {newNamer.UrlMap(lbName), &NameComponents{ClusterName: uid, Resource: "um", LbNamePrefix: "key1"}}, } { nc := newNamer.ParseName(tc.in) if *nc != *tc.want { @@ -294,7 +294,7 @@ func TestNamerLoadBalancer(t *testing.T) { for _, tc := range []struct { prefix string - lbName string + lbName LoadBalancerName targetHTTPProxy string targetHTTPSProxy string sslCert string @@ -304,7 +304,7 @@ func TestNamerLoadBalancer(t *testing.T) { }{ { "k8s", - "key1--uid1", + LoadBalancerName("key1--uid1"), "k8s-tp-key1--uid1", "k8s-tps-key1--uid1", "k8s-ssl-%s-%s--uid1", @@ -314,7 +314,7 @@ func TestNamerLoadBalancer(t *testing.T) { }, { "mci", - "key1--uid1", + LoadBalancerName("key1--uid1"), "mci-tp-key1--uid1", "mci-tps-key1--uid1", "mci-ssl-%s-%s--uid1",