Skip to content

Commit

Permalink
Use utilyaml to collect agent manifests in remote cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Aug 11, 2023
1 parent 15d3cdd commit a60da79
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 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
59 changes: 12 additions & 47 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 @@ -37,6 +36,7 @@ import (
"github.com/rancher-sandbox/rancher-turtles/internal/rancher"
turtlesannotations "github.com/rancher-sandbox/rancher-turtles/util/annotations"
turtlespredicates "github.com/rancher-sandbox/rancher-turtles/util/predicates"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"

Check failure on line 39 in internal/controllers/import_controller.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s blank -s dot -s default -s prefix(sigs.k8s.io/cluster-api) -s prefix(github.com/rancher-sandbox/rancher-turtles) --custom-order (gci)
)

const (
Expand Down Expand Up @@ -472,69 +472,34 @@ 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); err != nil {

Check failure on line 481 in internal/controllers/import_controller.go

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)

Check failure on line 481 in internal/controllers/import_controller.go

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
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)
err := c.Create(ctx, obj)
if client.IgnoreAlreadyExists(err) != nil {
return fmt.Errorf("creating object in remote cluster: %w", err)
}

if err == nil {
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 {
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

Check failure on line 504 in internal/controllers/import_controller.go

View workflow job for this annotation

GitHub Actions / lint

return statements should not be cuddled if block has more than two lines (wsl)

Check failure on line 504 in internal/controllers/import_controller.go

View workflow job for this annotation

GitHub Actions / lint

return statements should not be cuddled if block has more than two lines (wsl)
}

0 comments on commit a60da79

Please sign in to comment.