Skip to content

Commit

Permalink
WIP: Add resource hash annotation to Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamia authored and alexgeorgousis committed Jan 10, 2025
1 parent ecd9ad0 commit aa9ce00
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
11 changes: 11 additions & 0 deletions apis/pipelines/object_hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package pipelines

import (
"crypto/sha1"
"encoding/json"
"hash"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sort"
)

Expand Down Expand Up @@ -38,6 +40,15 @@ func (oh ObjectHasher) WriteMapField(value map[string]string) {
oh.WriteFieldSeparator()
}

func (oh ObjectHasher) WriteObject(obj metav1.Object) error {
bytes, err := json.Marshal(obj)
if err != nil {
return err
}
oh.h.Write(bytes)
return nil
}

type KV interface {
GetKey() string
GetValue() string
Expand Down
55 changes: 45 additions & 10 deletions controllers/pipelines/provider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
)

const (
OwnerNameLabel = "owner-name"
AppLabel = "app"
ResourceHashAnnotation = "resource-hash"
)

type ProviderReconciler struct {
StateHandler[*pipelinesv1.Provider]
ResourceReconciler[*pipelinesv1.Provider]
Expand Down Expand Up @@ -63,19 +69,28 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}

deploy, err := r.getDeployment(ctx, req.Namespace, provider.Name, *provider)
existingDeployment, err := r.getDeployment(ctx, req.Namespace, provider.Name, *provider)
if err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "unable to get existing deployment")
return ctrl.Result{}, err
}

logger.Info("desired provider deployment", "deployment", desiredDeployment)

if deploy != nil {
logger.Info("found existing provider deployment", "deployment", deploy)
//TODO: implement updating existing deployment
if existingDeployment != nil {
logger.Info("found existing provider deployment", "deployment", existingDeployment)
if existingDeployment.Annotations != nil && existingDeployment.Annotations[ResourceHashAnnotation] != desiredDeployment.Annotations[ResourceHashAnnotation] {
logger.Info("resource hash mismatch, updating deployment")
existingDeployment.Spec = desiredDeployment.Spec
existingDeployment.SetLabels(desiredDeployment.Labels)
existingDeployment.Annotations[ResourceHashAnnotation] = desiredDeployment.Annotations[ResourceHashAnnotation]
if err = r.EC.Client.Update(ctx, existingDeployment); err != nil {
logger.Error(err, "unable to update provider service deployment", "deployment", desiredDeployment)
return ctrl.Result{}, err
}
}
} else {
if err := r.EC.Client.Create(ctx, desiredDeployment); err != nil {
if err = r.EC.Client.Create(ctx, desiredDeployment); err != nil {
logger.Error(err, "unable to create provider service deployment")
return ctrl.Result{}, err
}
Expand All @@ -101,7 +116,7 @@ func (r *ProviderReconciler) getDeployment(ctx context.Context, namespace string
dl := &appsv1.DeploymentList{}
err := r.EC.Client.NonCached.List(ctx, dl, &client.ListOptions{
Namespace: namespace,
LabelSelector: labelSelector(map[string]string{"owner-name": fmt.Sprintf("provider-%s", providerName)}),
LabelSelector: labelSelector(map[string]string{OwnerNameLabel: fmt.Sprintf("provider-%s", providerName)}),
})

if err != nil {
Expand All @@ -120,8 +135,8 @@ func labelSelector(labelMap map[string]string) labels.Selector {
}

func (r *ProviderReconciler) constructDeployment(provider *pipelinesv1.Provider, namespace string, config config.KfpControllerConfigSpec) (*appsv1.Deployment, error) {
matchLabels := map[string]string{"app": fmt.Sprintf("provider-%s", provider.Name)}
ownerLabels := map[string]string{"owner-name": fmt.Sprintf("provider-%s", provider.Name)}
matchLabels := map[string]string{AppLabel: fmt.Sprintf("provider-%s", provider.Name)}
ownerLabels := map[string]string{OwnerNameLabel: fmt.Sprintf("provider-%s", provider.Name)}
deploymentLabels := pipelines.MapConcat(pipelines.MapConcat(config.DefaultProviderValues.Labels, matchLabels), ownerLabels)
replicas := int32(config.DefaultProviderValues.Replicas)

Expand All @@ -148,10 +163,15 @@ func (r *ProviderReconciler) constructDeployment(provider *pipelinesv1.Provider,
Template: podTemplate,
},
}
err := ctrl.SetControllerReference(provider, deployment, r.Scheme)
if err != nil {

if err := ctrl.SetControllerReference(provider, deployment, r.Scheme); err != nil {
return nil, err
}

if err := setResourceHashAnnotation(deployment); err != nil {
return nil, err
}

return deployment, nil
}

Expand All @@ -168,3 +188,18 @@ func populateMainContainer(podTemplate v1.PodTemplateSpec, provider *pipelinesv1
}
return podTemplate
}

func setResourceHashAnnotation(deployment *appsv1.Deployment) error {
hasher := pipelines.NewObjectHasher()
err := hasher.WriteObject(deployment)
if err != nil {
return err
}

if deployment.Annotations == nil {
deployment.Annotations = make(map[string]string)
}
deployment.Annotations[ResourceHashAnnotation] = string(hasher.Sum())

return nil
}

0 comments on commit aa9ce00

Please sign in to comment.