-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use v1 Ingresses when available (#182)
- Loading branch information
1 parent
4f12f49
commit 41b9b04
Showing
23 changed files
with
649 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2020 Nexus Operator and/or its authors | ||
// | ||
// 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 networking | ||
|
||
import ( | ||
"k8s.io/api/networking/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
"github.com/m88i/nexus-operator/api/v1alpha1" | ||
"github.com/m88i/nexus-operator/controllers/nexus/resource/deployment" | ||
"github.com/m88i/nexus-operator/controllers/nexus/resource/meta" | ||
) | ||
|
||
type legacyIngressBuilder struct { | ||
*v1beta1.Ingress | ||
nexus *v1alpha1.Nexus | ||
} | ||
|
||
func newLegacyIngressBuilder(nexus *v1alpha1.Nexus) *legacyIngressBuilder { | ||
ingress := &v1beta1.Ingress{ | ||
ObjectMeta: meta.DefaultObjectMeta(nexus), | ||
Spec: v1beta1.IngressSpec{ | ||
Rules: []v1beta1.IngressRule{ | ||
{ | ||
Host: nexus.Spec.Networking.Host, | ||
IngressRuleValue: v1beta1.IngressRuleValue{ | ||
HTTP: &v1beta1.HTTPIngressRuleValue{ | ||
Paths: []v1beta1.HTTPIngressPath{ | ||
{ | ||
Path: ingressBasePath, | ||
Backend: v1beta1.IngressBackend{ | ||
ServiceName: nexus.Name, | ||
ServicePort: intstr.FromInt(deployment.NexusServicePort), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return &legacyIngressBuilder{Ingress: ingress, nexus: nexus} | ||
} | ||
|
||
func (i *legacyIngressBuilder) withCustomTLS() *legacyIngressBuilder { | ||
i.Spec.TLS = []v1beta1.IngressTLS{ | ||
{ | ||
Hosts: legacyHosts(i.Spec.Rules), | ||
SecretName: i.nexus.Spec.Networking.TLS.SecretName, | ||
}, | ||
} | ||
return i | ||
} | ||
|
||
func (i *legacyIngressBuilder) build() *v1beta1.Ingress { | ||
return i.Ingress | ||
} | ||
|
||
func legacyHosts(rules []v1beta1.IngressRule) []string { | ||
var hosts []string | ||
for _, rule := range rules { | ||
hosts = append(hosts, rule.Host) | ||
} | ||
return hosts | ||
} |
89 changes: 89 additions & 0 deletions
89
controllers/nexus/resource/networking/legacy_ingress_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2020 Nexus Operator and/or its authors | ||
// | ||
// 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 networking | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/api/networking/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
"github.com/m88i/nexus-operator/controllers/nexus/resource/deployment" | ||
) | ||
|
||
func TestLegacyHosts(t *testing.T) { | ||
ingress := &v1beta1.Ingress{ | ||
Spec: v1beta1.IngressSpec{ | ||
Rules: []v1beta1.IngressRule{}, | ||
}, | ||
} | ||
|
||
h := legacyHosts(ingress.Spec.Rules) | ||
assert.Len(t, h, 0) | ||
|
||
host := "a" | ||
ingress.Spec.Rules = append(ingress.Spec.Rules, v1beta1.IngressRule{Host: host}) | ||
h = legacyHosts(ingress.Spec.Rules) | ||
assert.Len(t, h, 1) | ||
assert.Equal(t, h[0], host) | ||
|
||
host = "b" | ||
ingress.Spec.Rules = append(ingress.Spec.Rules, v1beta1.IngressRule{Host: host}) | ||
h = legacyHosts(ingress.Spec.Rules) | ||
assert.Len(t, h, 2) | ||
assert.Equal(t, h[1], host) | ||
} | ||
|
||
func TestNewLegacyIngress(t *testing.T) { | ||
ingress := newLegacyIngressBuilder(ingressNexus).build() | ||
assertLegacyIngressBasic(t, ingress) | ||
} | ||
|
||
func TestNewLegacyIngressWithSecretName(t *testing.T) { | ||
ingress := newLegacyIngressBuilder(ingressNexus).withCustomTLS().build() | ||
assertLegacyIngressBasic(t, ingress) | ||
assertLegacyIngressSecretName(t, ingress) | ||
} | ||
|
||
func assertLegacyIngressBasic(t *testing.T, ingress *v1beta1.Ingress) { | ||
assert.Equal(t, ingressNexus.Name, ingress.Name) | ||
assert.Equal(t, ingressNexus.Namespace, ingress.Namespace) | ||
|
||
assert.NotNil(t, ingress.Spec) | ||
|
||
assert.Len(t, ingress.Spec.Rules, 1) | ||
rule := ingress.Spec.Rules[0] | ||
|
||
assert.Equal(t, ingressNexus.Spec.Networking.Host, rule.Host) | ||
assert.NotNil(t, rule.IngressRuleValue) | ||
assert.NotNil(t, rule.IngressRuleValue.HTTP) | ||
|
||
assert.Len(t, rule.IngressRuleValue.HTTP.Paths, 1) | ||
path := rule.IngressRuleValue.HTTP.Paths[0] | ||
|
||
assert.Equal(t, ingressBasePath, path.Path) | ||
assert.NotNil(t, path.Backend) | ||
assert.Equal(t, intstr.FromInt(deployment.NexusServicePort), path.Backend.ServicePort) | ||
assert.Equal(t, ingressNexus.Name, path.Backend.ServiceName) | ||
} | ||
|
||
func assertLegacyIngressSecretName(t *testing.T, ingress *v1beta1.Ingress) { | ||
assert.Len(t, ingress.Spec.TLS, 1) | ||
assert.Equal(t, ingressNexus.Spec.Networking.TLS.SecretName, ingress.Spec.TLS[0].SecretName) | ||
|
||
assert.Len(t, ingress.Spec.TLS[0].Hosts, 1) | ||
assert.Equal(t, ingressNexus.Spec.Networking.Host, ingress.Spec.TLS[0].Hosts[0]) | ||
} |
Oops, something went wrong.