Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Use utilyaml to collect agent manifests in remote cluster #71

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}