forked from knative/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the label and annotation overwriting for service (knative#1033
) * Add the ServiceDeployment into the APIs * Implement the ServicesTransformer * Update the deps and manifests * Update the CRDs with the new field services
- Loading branch information
Vincent
authored
Apr 12, 2022
1 parent
fb31b0a
commit f7f71c5
Showing
7 changed files
with
278 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,73 @@ | ||
/* | ||
Copyright 2022 The Knative 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 common | ||
|
||
import ( | ||
mf "github.com/manifestival/manifestival" | ||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"knative.dev/operator/pkg/apis/operator/base" | ||
) | ||
|
||
// ServicesTransform transforms services based on the configuration in `spec.service`. | ||
func ServicesTransform(obj base.KComponent, log *zap.SugaredLogger) mf.Transformer { | ||
overrides := obj.GetSpec().GetServiceOverride() | ||
if overrides == nil { | ||
return nil | ||
} | ||
return func(u *unstructured.Unstructured) error { | ||
for _, override := range overrides { | ||
if u.GetKind() == "Service" && u.GetName() == override.Name { | ||
service := &corev1.Service{} | ||
if err := scheme.Scheme.Convert(u, service, nil); err != nil { | ||
return err | ||
} | ||
overrideLabels(&override, service) | ||
overrideAnnotations(&override, service) | ||
if err := scheme.Scheme.Convert(service, u, nil); err != nil { | ||
return err | ||
} | ||
// Avoid superfluous updates from converted zero defaults | ||
u.SetCreationTimestamp(metav1.Time{}) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func overrideAnnotations(override *base.ServiceOverride, service *corev1.Service) { | ||
if service.GetAnnotations() == nil { | ||
service.Annotations = map[string]string{} | ||
} | ||
|
||
for key, val := range override.Annotations { | ||
service.Annotations[key] = val | ||
} | ||
} | ||
|
||
func overrideLabels(override *base.ServiceOverride, service *corev1.Service) { | ||
if service.GetLabels() == nil { | ||
service.Labels = map[string]string{} | ||
} | ||
|
||
for key, val := range override.Labels { | ||
service.Labels[key] = val | ||
} | ||
} |
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,103 @@ | ||
/* | ||
Copyright 2022 The Knative 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 common | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
mf "github.com/manifestival/manifestival" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"knative.dev/operator/pkg/apis/operator/base" | ||
servingv1beta1 "knative.dev/operator/pkg/apis/operator/v1beta1" | ||
) | ||
|
||
type expServices struct { | ||
expLabels map[string]string | ||
expAnnotations map[string]string | ||
} | ||
|
||
func TestServicesTransform(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
override []base.ServiceOverride | ||
expServices map[string]expServices | ||
}{{ | ||
name: "simple override", | ||
override: []base.ServiceOverride{ | ||
{ | ||
Name: "controller", | ||
Labels: map[string]string{"a": "b"}, | ||
Annotations: map[string]string{"c": "d"}, | ||
}, | ||
}, | ||
expServices: map[string]expServices{"controller": { | ||
expLabels: map[string]string{"serving.knative.dev/release": "v0.13.0", "a": "b", "app": "controller"}, | ||
expAnnotations: map[string]string{"c": "d"}, | ||
}}, | ||
}, { | ||
name: "no override", | ||
override: nil, | ||
expServices: map[string]expServices{"controller": { | ||
expLabels: map[string]string{"serving.knative.dev/release": "v0.13.0", "app": "controller"}, | ||
expAnnotations: nil, | ||
}}, | ||
}} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
manifest, err := mf.NewManifest("testdata/manifest.yaml") | ||
if err != nil { | ||
t.Fatalf("Failed to create manifest: %v", err) | ||
} | ||
|
||
ks := &servingv1beta1.KnativeServing{ | ||
Spec: servingv1beta1.KnativeServingSpec{ | ||
CommonSpec: base.CommonSpec{ | ||
ServiceOverride: test.override, | ||
}, | ||
}, | ||
} | ||
|
||
manifest, err = manifest.Transform(ServicesTransform(ks, log)) | ||
if err != nil { | ||
t.Fatalf("Failed to transform manifest: %v", err) | ||
} | ||
|
||
for expName, d := range test.expServices { | ||
for _, u := range manifest.Resources() { | ||
if u.GetKind() == "Service" && u.GetName() == expName { | ||
got := &corev1.Service{} | ||
if err := scheme.Scheme.Convert(&u, got, nil); err != nil { | ||
t.Fatalf("Failed to convert unstructured to deployment: %v", err) | ||
} | ||
|
||
if diff := cmp.Diff(got.GetLabels(), d.expLabels); diff != "" { | ||
t.Fatalf("Unexpected labels: %v", diff) | ||
} | ||
|
||
if diff := cmp.Diff(got.GetAnnotations(), d.expAnnotations); diff != "" { | ||
t.Fatalf("Unexpected annotations: %v", diff) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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