-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from inteon/add_versionchecker
Move versionchecker from cert-manager/cert-manager to this repo + refactor tests
- Loading branch information
Showing
12 changed files
with
11,310 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2021 The cert-manager 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 versionchecker | ||
|
||
import ( | ||
"context" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (o *VersionChecker) extractVersionFromCrd(ctx context.Context, crdName string) error { | ||
crdKey := client.ObjectKey{Name: crdName} | ||
|
||
objv1 := &apiextensionsv1.CustomResourceDefinition{} | ||
err := o.client.Get(ctx, crdKey, objv1) | ||
if err == nil { | ||
if label := extractVersionFromLabels(objv1.Labels); label != "" { | ||
o.versionSources["crdLabelVersion"] = label | ||
} | ||
|
||
return o.extractVersionFromCrdv1(ctx, objv1) | ||
} | ||
|
||
// If error differs from not found, don't continue and return error | ||
if !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
|
||
objv1beta1 := &apiextensionsv1beta1.CustomResourceDefinition{} | ||
err = o.client.Get(ctx, crdKey, objv1beta1) | ||
if err == nil { | ||
if label := extractVersionFromLabels(objv1beta1.Labels); label != "" { | ||
o.versionSources["crdLabelVersion"] = label | ||
} | ||
|
||
return o.extractVersionFromCrdv1beta1(ctx, objv1beta1) | ||
} | ||
|
||
// If error differs from not found, don't continue and return error | ||
if !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
|
||
return ErrCertManagerCRDsNotFound | ||
} | ||
|
||
func (o *VersionChecker) extractVersionFromCrdv1(ctx context.Context, crd *apiextensionsv1.CustomResourceDefinition) error { | ||
if (crd.Spec.Conversion == nil) || | ||
(crd.Spec.Conversion.Webhook == nil) || | ||
(crd.Spec.Conversion.Webhook.ClientConfig == nil) || | ||
(crd.Spec.Conversion.Webhook.ClientConfig.Service == nil) { | ||
return nil | ||
} | ||
|
||
return o.extractVersionFromService( | ||
ctx, | ||
crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace, | ||
crd.Spec.Conversion.Webhook.ClientConfig.Service.Name, | ||
) | ||
} | ||
|
||
func (o *VersionChecker) extractVersionFromCrdv1beta1(ctx context.Context, crd *apiextensionsv1beta1.CustomResourceDefinition) error { | ||
if (crd.Spec.Conversion == nil) || | ||
(crd.Spec.Conversion.WebhookClientConfig == nil) || | ||
(crd.Spec.Conversion.WebhookClientConfig.Service == nil) { | ||
return nil | ||
} | ||
|
||
return o.extractVersionFromService( | ||
ctx, | ||
crd.Spec.Conversion.WebhookClientConfig.Service.Namespace, | ||
crd.Spec.Conversion.WebhookClientConfig.Service.Name, | ||
) | ||
} |
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,45 @@ | ||
/* | ||
Copyright 2021 The cert-manager 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 versionchecker | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var helmChartVersion = regexp.MustCompile(`-(v(?:\d+)\.(?:\d+)\.(?:\d+)(?:.*))$`) | ||
|
||
func extractVersionFromLabels(crdLabels map[string]string) string { | ||
if version, ok := crdLabels["app.kubernetes.io/version"]; ok { | ||
return version | ||
} | ||
|
||
if chartName, ok := crdLabels["helm.sh/chart"]; ok { | ||
version := helmChartVersion.FindStringSubmatch(chartName) | ||
if len(version) == 2 { | ||
return version[1] | ||
} | ||
} | ||
|
||
if chartName, ok := crdLabels["chart"]; ok { | ||
version := helmChartVersion.FindStringSubmatch(chartName) | ||
if len(version) == 2 { | ||
return version[1] | ||
} | ||
} | ||
|
||
return "" | ||
} |
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,74 @@ | ||
/* | ||
Copyright 2021 The cert-manager 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 versionchecker | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var imageVersion = regexp.MustCompile(`^quay.io/jetstack/cert-manager-webhook:(v(?:\d+)\.(?:\d+)\.(?:\d+)(?:.*))$`) | ||
|
||
func (o *VersionChecker) extractVersionFromService( | ||
ctx context.Context, | ||
namespace string, | ||
serviceName string, | ||
) error { | ||
service := &corev1.Service{} | ||
serviceKey := client.ObjectKey{Namespace: namespace, Name: serviceName} | ||
err := o.client.Get(ctx, serviceKey, service) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if label := extractVersionFromLabels(service.Labels); label != "" { | ||
o.versionSources["webhookServiceLabelVersion"] = label | ||
} | ||
|
||
listOptions := client.MatchingLabelsSelector{ | ||
Selector: labels.Set(service.Spec.Selector).AsSelector(), | ||
} | ||
pods := &corev1.PodList{} | ||
err = o.client.List(ctx, pods, listOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, pod := range pods.Items { | ||
if pod.Status.Phase != corev1.PodRunning { | ||
continue | ||
} | ||
|
||
if label := extractVersionFromLabels(pod.Labels); label != "" { | ||
o.versionSources["webhookPodLabelVersion"] = label | ||
} | ||
|
||
for _, container := range pod.Spec.Containers { | ||
version := imageVersion.FindStringSubmatch(container.Image) | ||
if len(version) == 2 { | ||
o.versionSources["webhookPodImageVersion"] = version[1] | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,93 @@ | ||
/* | ||
Copyright 2021 The cert-manager 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 versionchecker | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/api/validation" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
) | ||
|
||
// Based on https://github.com/kubernetes/kubernetes/blob/ca643a4d1f7bfe34773c74f79527be4afd95bf39/pkg/controller/controller_utils.go#L542 | ||
|
||
var validatePodName = validation.NameIsDNSSubdomain | ||
|
||
func getPodFromTemplate(template *v1.PodTemplateSpec, parentObject runtime.Object, controllerRef *metav1.OwnerReference) (*v1.Pod, error) { | ||
desiredLabels := getPodsLabelSet(template) | ||
desiredFinalizers := getPodsFinalizers(template) | ||
desiredAnnotations := getPodsAnnotationSet(template) | ||
accessor, err := meta.Accessor(parentObject) | ||
if err != nil { | ||
return nil, fmt.Errorf("parentObject does not have ObjectMeta, %v", err) | ||
} | ||
prefix := getPodsPrefix(accessor.GetName()) | ||
|
||
pod := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: desiredLabels, | ||
Annotations: desiredAnnotations, | ||
GenerateName: prefix, | ||
Name: prefix + rand.String(5), | ||
Finalizers: desiredFinalizers, | ||
}, | ||
Status: v1.PodStatus{ | ||
Phase: v1.PodRunning, | ||
}, | ||
} | ||
if controllerRef != nil { | ||
pod.OwnerReferences = append(pod.OwnerReferences, *controllerRef) | ||
} | ||
pod.Spec = *template.Spec.DeepCopy() | ||
return pod, nil | ||
} | ||
|
||
func getPodsLabelSet(template *v1.PodTemplateSpec) labels.Set { | ||
desiredLabels := make(labels.Set) | ||
for k, v := range template.Labels { | ||
desiredLabels[k] = v | ||
} | ||
return desiredLabels | ||
} | ||
|
||
func getPodsFinalizers(template *v1.PodTemplateSpec) []string { | ||
desiredFinalizers := make([]string, len(template.Finalizers)) | ||
copy(desiredFinalizers, template.Finalizers) | ||
return desiredFinalizers | ||
} | ||
|
||
func getPodsAnnotationSet(template *v1.PodTemplateSpec) labels.Set { | ||
desiredAnnotations := make(labels.Set) | ||
for k, v := range template.Annotations { | ||
desiredAnnotations[k] = v | ||
} | ||
return desiredAnnotations | ||
} | ||
|
||
func getPodsPrefix(controllerName string) string { | ||
// use the dash (if the name isn't too long) to make the pod name a bit prettier | ||
prefix := fmt.Sprintf("%s-", controllerName) | ||
if len(validatePodName(prefix, true)) != 0 { | ||
prefix = controllerName | ||
} | ||
return prefix | ||
} |
Oops, something went wrong.