in operator/pkg/kubernetes/apply.go [101:145]
func (a *Application) apply(ctx context.Context, obj *unstructured.Unstructured, log logr.Logger, needCompose bool) (bool, error) {
key := client.ObjectKeyFromObject(obj)
current := &unstructured.Unstructured{}
current.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
err := a.Client.Get(ctx, key, current)
if apierrors.IsNotFound(err) {
log.Info("could not find existing resource, creating one...")
if needCompose {
curr, errComp := a.compose(obj)
if errComp != nil {
return false, fmt.Errorf("failed to compose: %w", errComp)
}
obj = curr
}
if err = a.Client.Create(ctx, obj); err != nil {
return false, fmt.Errorf("failed to create: %w", err)
}
log.Info("created")
return true, nil
}
if err != nil {
return false, fmt.Errorf("failed to get %v : %w", key, err)
}
if needCompose {
object, err := a.compose(obj)
if err != nil {
return false, fmt.Errorf("failed to compose: %w", err)
}
obj = object
}
if getVersion(current, a.versionKey()) == getVersion(obj, a.versionKey()) {
log.Info("resource keeps the same as before")
return false, nil
}
if err := a.Client.Update(ctx, obj); err != nil {
return false, fmt.Errorf("failed to update: %w", err)
}
log.Info("updated")
return true, nil
}