Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Sep 30, 2024
1 parent 45ade39 commit eddffb1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
7 changes: 4 additions & 3 deletions hack/diff/compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ function get_state() {

mkdir -p "$dir_name"

while read -r res_type res_name
while read -r res_type res_name api_version
do
kubectl get "$res_type" "$res_name" -oyaml > "$dir_name/$res_type-$res_name.yaml"
done <<< "$(kubectl get "$type" "$name" -oyaml | yq -r '.spec.resourceRefs | .[] | .kind + " " + .name')"
group=$(echo "$api_version" | cut -d "/" -f1)
kubectl get "$res_type"."$group" "$res_name" -oyaml > "$dir_name/$res_type-$res_name.yaml"
done <<< "$(kubectl get "$type" "$name" -oyaml | yq -r '.spec.resourceRefs | .[] | .kind + " " + .name + " " + .apiVersion')"

}

Expand Down
70 changes: 62 additions & 8 deletions pkg/comp-functions/runtime/function_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ var (
)

const (
OwnerKindAnnotation = "appcat.vshn.io/ownerkind"
OwnerVersionAnnotation = "appcat.vshn.io/ownerapiversion"
OwnerGroupAnnotation = "appcat.vshn.io/ownergroup"
ProtectedByAnnotation = "appcat.vshn.io/protectedby"
ProtectsAnnotation = "appcat.vshn.io/protects"
EventForwardAnnotation = "appcat.vshn.io/forward-events-to"
providerConfigLabel = "appcat.vshn.io/provider-config"
OwnerKindAnnotation = "appcat.vshn.io/ownerkind"
OwnerVersionAnnotation = "appcat.vshn.io/ownerapiversion"
OwnerGroupAnnotation = "appcat.vshn.io/ownergroup"
ProtectedByAnnotation = "appcat.vshn.io/protectedby"
ProtectsAnnotation = "appcat.vshn.io/protects"
EventForwardAnnotation = "appcat.vshn.io/forward-events-to"
providerConfigLabel = "appcat.vshn.io/provider-config"
providerConfigIgnoreLabel = "appcat.vshn.io/ignore-config"
)

// Step describes a single change within a service.
Expand Down Expand Up @@ -1094,6 +1095,14 @@ func (s *ServiceRuntime) UsageOfBy(of, by string) error {
ofAPIVersion, ofKind := ofUnstructured.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
byAPIVersion, byKind := byUnstructured.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()

changed, err := s.unwrapUsage(name)
if err != nil {
return fmt.Errorf("cannot remove kube object wrapper from uage: %w", err)
}
if changed {
return nil
}

usage := &xpapi.Usage{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -1117,7 +1126,46 @@ func (s *ServiceRuntime) UsageOfBy(of, by string) error {
},
}

return s.SetDesiredKubeObject(usage, name)
composedRes, err := composed.From(usage)
if err != nil {
return fmt.Errorf("cannot convert usage object to managed resource: %w", err)
}

s.desiredResources[resource.Name(name)] = &resource.DesiredComposed{Resource: composedRes}

return nil
}

// unwrapUsage will check if the usage is wrapped into an object. If so it will
// set the deletionPolicy of the object to `orphan`. If the deletionPolicy is
// `orphan`, then it will be omitted from the desired state.
func (s *ServiceRuntime) unwrapUsage(name string) (bool, error) {
usage := &xkube.Object{}
err := s.GetObservedComposedResource(usage, name)
if err != nil {
if err == ErrNotFound {
return false, nil
}
return false, err
}

// we need to clean the objectmeta, or Crossplane will struggle with adding
// it to the `resourceRefs` array.
usage.ObjectMeta = metav1.ObjectMeta{
Name: usage.GetName(),
}

if len(usage.Spec.ManagementPolicies) == 0 || usage.Spec.ManagementPolicies[0] != xpv1.ManagementActionObserve {
usage.Spec.ManagementPolicies = xpv1.ManagementPolicies{
xpv1.ManagementActionObserve,
}
err := s.SetDesiredComposedResourceWithName(usage, name)
if err != nil {
return false, err
}
return true, nil
}
return false, nil
}

func (s *ServiceRuntime) addUsages() error {
Expand Down Expand Up @@ -1223,10 +1271,16 @@ func (s *ServiceRuntime) setProviderConfigs() error {
configName := s.observedComposite.GetLabels()[providerConfigLabel]

for i := range s.desiredResources {
if _, exists := s.desiredResources[i].Resource.GetLabels()[providerConfigIgnoreLabel]; exists {
continue
}
// we set the providerConfig Ref
err := s.desiredResources[i].Resource.SetString("spec.providerConfigRef.name", configName)
if err != nil {
return fmt.Errorf("cannot set providerConfig for %s: %w", s.desiredResources[i].Resource.GetName(), err)
}

//TODO: and also the provider-config label in case it's a nested composite
}

return nil
Expand Down

0 comments on commit eddffb1

Please sign in to comment.