Skip to content

Commit

Permalink
Merge pull request #71 from Danil-Grigorev/use-capi-unstructured-util
Browse files Browse the repository at this point in the history
🌱 Use utilyaml to collect agent manifests in remote cluster
  • Loading branch information
alexander-demicev authored Aug 24, 2023
2 parents fc33a05 + 2cf0595 commit a805f3c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
)

require (
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
Expand Down
56 changes: 11 additions & 45 deletions internal/controllers/import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -32,6 +31,7 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/util/predicates"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"

"github.com/rancher-sandbox/rancher-turtles/internal/rancher"
turtlesannotations "github.com/rancher-sandbox/rancher-turtles/util/annotations"
Expand Down Expand Up @@ -470,69 +470,35 @@ func (r *CAPIImportReconciler) createImportManifest(ctx context.Context, remoteC
}

func (r *CAPIImportReconciler) createRawManifest(ctx context.Context, remoteClient client.Client, bytes []byte) error {
bytes, err := yamlDecoder.ToJSON(bytes)
items, err := utilyaml.ToUnstructured(bytes)
if err != nil {
return err
}

check := map[string]interface{}{}
if err := json.Unmarshal(bytes, &check); err != nil {
return fmt.Errorf("error unmarshalling bytes or empty object passed: %w", err)
}

// return if object is empty
if len(check) == 0 {
return nil
}

obj, _, err := unstructured.UnstructuredJSONScheme.Decode(bytes, nil, nil)
if err != nil {
return err
}

switch obj := obj.(type) {
case *unstructured.Unstructured:
if err := r.createObject(ctx, remoteClient, obj); err != nil {
for _, obj := range items {
if err := r.createObject(ctx, remoteClient, obj.DeepCopy()); err != nil {
return err
}

return nil
case *unstructured.UnstructuredList:
for i := range obj.Items {
if err := r.createObject(ctx, remoteClient, &obj.Items[i]); err != nil {
return err
}
}

return nil
default:
return fmt.Errorf("unknown object type: %T", obj)
}

return nil
}

func (r *CAPIImportReconciler) createObject(ctx context.Context, c client.Client, obj client.Object) error {
log := log.FromContext(ctx)
gvk := obj.GetObjectKind().GroupVersionKind()

u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)
err := c.Get(ctx, client.ObjectKey{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}, u)

if err == nil {
err := c.Create(ctx, obj)
if apierrors.IsAlreadyExists(err) {
log.V(4).Info("object already exists in remote cluster", "gvk", gvk, "name", obj.GetName(), "namespace", obj.GetNamespace())
return nil
}

if !apierrors.IsNotFound(err) {
return fmt.Errorf("getting object from remote cluster: %w", err)
}

if err := c.Create(ctx, obj); err != nil {
if err != nil {
return fmt.Errorf("creating object in remote cluster: %w", err)
}

log.V(4).Info("object was created", "gvk", gvk, "name", obj.GetName(), "namespace", obj.GetNamespace())

return nil
}

0 comments on commit a805f3c

Please sign in to comment.