Skip to content

Commit

Permalink
replace exec with client create
Browse files Browse the repository at this point in the history
  • Loading branch information
cahillsf committed May 11, 2024
1 parent fd328d7 commit 2bff764
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
6 changes: 3 additions & 3 deletions test/e2e/clusterclass_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func assertControlPlane(g Gomega, clusterClassObjects clusterClassObjects, clust
clusterv1.TemplateClonedFromNameAnnotation: clusterClass.Spec.ControlPlane.MachineInfrastructure.Ref.Name,
},
clusterClassObjects.ControlPlaneInfrastructureMachineTemplate.GetAnnotations(),
).without(g, corev1.LastAppliedConfigAnnotation),
),
))

// ControlPlane InfrastructureMachineTemplate.spec.template.metadata
Expand Down Expand Up @@ -587,7 +587,7 @@ func assertMachineDeployments(g Gomega, clusterClassObjects clusterClassObjects,
clusterv1.TemplateClonedFromNameAnnotation: mdClass.Template.Infrastructure.Ref.Name,
},
ccInfrastructureMachineTemplate.GetAnnotations(),
).without(g, corev1.LastAppliedConfigAnnotation),
),
))
// MachineDeployment InfrastructureMachineTemplate.spec.template.metadata
g.Expect(infrastructureMachineTemplateTemplateMetadata.Labels).To(BeEquivalentTo(
Expand Down Expand Up @@ -619,7 +619,7 @@ func assertMachineDeployments(g Gomega, clusterClassObjects clusterClassObjects,
clusterv1.TemplateClonedFromNameAnnotation: mdClass.Template.Bootstrap.Ref.Name,
},
ccBootstrapConfigTemplate.GetAnnotations(),
).without(g, corev1.LastAppliedConfigAnnotation),
),
))
// MachineDeployment BootstrapConfigTemplate.spec.template.metadata
g.Expect(bootstrapConfigTemplateTemplateMetadata.Labels).To(BeEquivalentTo(
Expand Down
77 changes: 68 additions & 9 deletions test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
goruntime "runtime"
"sync"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
pkgerrors "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -44,9 +47,9 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
testexec "sigs.k8s.io/cluster-api/test/framework/exec"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
"sigs.k8s.io/cluster-api/test/infrastructure/container"
"sigs.k8s.io/cluster-api/util/yaml"
)

const (
Expand Down Expand Up @@ -251,16 +254,72 @@ func (p *clusterProxy) GetCache(ctx context.Context) cache.Cache {
func (p *clusterProxy) Apply(ctx context.Context, resources []byte, args ...string) error {
Expect(ctx).NotTo(BeNil(), "ctx is required for Apply")
Expect(resources).NotTo(BeNil(), "resources is required for Apply")

if err := testexec.KubectlApply(ctx, p.kubeconfigPath, resources, args...); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return pkgerrors.New(fmt.Sprintf("%s: stderr: %s", err.Error(), exitErr.Stderr))
argsDict := make(map[string]string)
for i := 0; i < len(args); i += 2 {
if i+1 < len(args) {
argsDict[args[i]] = args[i+1]
}
}
labelSelector := labels.Everything()
for k, v := range argsDict {
fmt.Printf("argsDict[%s]: %s\n", k, v)
if k == "--selector" {
ls, err := metav1.ParseToLabelSelector(v)
if err != nil {
return fmt.Errorf("could not parse selector: %+w", err)
}
labelSelector, err = metav1.LabelSelectorAsSelector(ls)
if err != nil {
return fmt.Errorf("could not convert metav1.LabelSelector to labels.Selector: %+w", err)
}
} else {
return fmt.Errorf(fmt.Sprintf("unsupported arg: %s\n", k))

Check failure on line 276 in test/framework/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

dynamicFmtString: use errors.New(fmt.Sprintf("unsupported arg: %s\n", k)) or fmt.Errorf("%s", fmt.Sprintf("unsupported arg: %s\n", k)) instead (gocritic)

Check failure on line 276 in test/framework/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

dynamicFmtString: use errors.New(fmt.Sprintf("unsupported arg: %s\n", k)) or fmt.Errorf("%s", fmt.Sprintf("unsupported arg: %s\n", k)) instead (gocritic)
}
}

var retErrs []error
objs, err := yaml.ToUnstructured(resources)
if err != nil {
return err
}

return nil
existingObject := &unstructured.Unstructured{}
for _, o := range objs {
o := o
objectKey := types.NamespacedName{
Name: o.GetName(),
Namespace: o.GetNamespace(),
}
existingObject.SetAPIVersion(o.GetAPIVersion())
existingObject.SetKind(o.GetKind())
fmt.Printf("Got object key: %+v\n", objectKey)

labels := labels.Set(o.GetLabels())
if labelSelector.Matches(labels) {
fmt.Printf("Object DID MATCH selector: %+v\n", o)
if err := p.GetClient().Get(ctx, objectKey, existingObject); err != nil {
fmt.Printf("Failed to get object %+v: %v\n", objectKey, err)
if err := p.GetClient().Create(ctx, &o); err != nil {
retErrs = append(retErrs, err)
fmt.Printf("Failed to create object %+v: %v\n", objectKey, err)
} else {
fmt.Printf("Created object %+v\n", objectKey)
}
} else {
o.SetResourceVersion(existingObject.GetResourceVersion())
if err := p.GetClient().Update(ctx, &o); err != nil {
retErrs = append(retErrs, err)
fmt.Printf("Failed to update object %+v: %v\n", objectKey, err)
} else {
fmt.Printf("Updated object %+v\n", objectKey)
}
}
} else {
fmt.Printf("Object did not match selector: %+v\n", o)
}

}

Check failure on line 321 in test/framework/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

unnecessary trailing newline (whitespace)

Check failure on line 321 in test/framework/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

unnecessary trailing newline (whitespace)
return kerrors.NewAggregate(retErrs)
}

func (p *clusterProxy) GetRESTConfig() *rest.Config {
Expand Down

0 comments on commit 2bff764

Please sign in to comment.