Skip to content

Commit

Permalink
minro fixes
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Sep 26, 2022
1 parent 79ce006 commit fe5e71e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
2 changes: 0 additions & 2 deletions operator/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ metadata:
annotations:
keptn.sh/application: "application12"
keptn.sh/service: "service-test"
keptn.sh/version: "1.1"
spec:
replicas: 3
selector:
Expand All @@ -20,7 +19,6 @@ spec:
annotations:
keptn.sh/application: "application12"
keptn.sh/service: "service-test"
keptn.sh/version: "1.1"
spec:
containers:
- name: nginx
Expand Down
39 changes: 22 additions & 17 deletions operator/webhooks/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ func (r *PodMutatingWebhook) handleService(ctx context.Context, logger logr.Logg
return nil
}

service = r.generateService(ctx, pod)
service.Spec.Version = pod.Annotations[common.VersionAnnotation]
service.Spec.ResourceReference = r.getResourceReference(pod)
service.Annotations[common.VersionAnnotation] = pod.Annotations[common.VersionAnnotation]
err = r.Client.Update(ctx, service)
if err != nil {
logger.Error(err, "Could not update Service")
Expand All @@ -156,29 +158,16 @@ func (r *PodMutatingWebhook) handleService(ctx context.Context, logger logr.Logg
func (r *PodMutatingWebhook) generateService(ctx context.Context, pod *corev1.Pod) *v1alpha1.Service {
version, _ := pod.Annotations[common.VersionAnnotation]
applicationName, _ := pod.Annotations[common.ApplicationAnnotation]
ownerUID := pod.UID
ownerKind := "Pod"
if len(pod.OwnerReferences) != 0 {
for _, o := range pod.OwnerReferences {
if o.Kind == "ReplicaSet" {
ownerUID = o.UID
ownerKind = o.Kind
}
}
}
return &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: pod.Annotations,
Name: r.getServiceName(pod),
Namespace: pod.Namespace,
},
Spec: v1alpha1.ServiceSpec{
ApplicationName: applicationName,
Version: version,
ResourceReference: v1alpha1.ResourceReference{
UID: ownerUID,
Kind: ownerKind,
},
ApplicationName: applicationName,
Version: version,
ResourceReference: r.getResourceReference(pod),
//for now hardcoded, will be changed in future
PreDeploymentCheck: v1alpha1.EventSpec{
Service: r.getServiceName(pod),
Expand Down Expand Up @@ -244,3 +233,19 @@ func (r *PodMutatingWebhook) getServiceName(pod *corev1.Pod) string {
applicationName, _ := pod.Annotations[common.ApplicationAnnotation]
return strings.ToLower(applicationName + "-" + serviceName)
}

func (r *PodMutatingWebhook) getResourceReference(pod *corev1.Pod) v1alpha1.ResourceReference {
reference := v1alpha1.ResourceReference{
UID: pod.UID,
Kind: pod.Kind,
}
if len(pod.OwnerReferences) != 0 {
for _, o := range pod.OwnerReferences {
if o.Kind == "ReplicaSet" {
reference.UID = o.UID
reference.Kind = o.Kind
}
}
}
return reference
}

0 comments on commit fe5e71e

Please sign in to comment.