From c8da9e89990c5cc706bb93026d91d258989f5cab Mon Sep 17 00:00:00 2001 From: panktishah26 Date: Tue, 10 May 2022 21:23:47 +0000 Subject: [PATCH] Modified tink hardware crd example Signed-off-by: panktishah26 --- cmd/tink-controller/main.go | 2 - config/crd/examples/hardware.yaml | 10 +- pkg/controllers/hardware/controller.go | 81 ----------- pkg/controllers/hardware/controller_test.go | 149 -------------------- 4 files changed, 2 insertions(+), 240 deletions(-) delete mode 100644 pkg/controllers/hardware/controller.go delete mode 100644 pkg/controllers/hardware/controller_test.go diff --git a/cmd/tink-controller/main.go b/cmd/tink-controller/main.go index 1bb1e6e49..62eb515a2 100644 --- a/cmd/tink-controller/main.go +++ b/cmd/tink-controller/main.go @@ -11,7 +11,6 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/tinkerbell/tink/pkg/controllers" - hwctrl "github.com/tinkerbell/tink/pkg/controllers/hardware" wfctrl "github.com/tinkerbell/tink/pkg/controllers/workflow" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" @@ -82,7 +81,6 @@ func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command { return manager.RegisterControllers( cmd.Context(), - hwctrl.NewController(manager.GetClient()), wfctrl.NewController(manager.GetClient()), ).Start(cmd.Context()) }, diff --git a/config/crd/examples/hardware.yaml b/config/crd/examples/hardware.yaml index 2c0b575f6..cd27c846d 100644 --- a/config/crd/examples/hardware.yaml +++ b/config/crd/examples/hardware.yaml @@ -4,6 +4,8 @@ metadata: name: sm01 namespace: default spec: + disks: + - device: /dev/nvme0n1 metadata: facility: facility_code: onprem @@ -17,14 +19,6 @@ spec: distro: "ubuntu" os_slug: "ubuntu_20_04" version: "20.04" - storage: - disks: - - device: /dev/nvme0n1 - partitions: - - label: ROOT - number: 1 - size: 0 - wipe_table: true interfaces: - dhcp: arch: x86_64 diff --git a/pkg/controllers/hardware/controller.go b/pkg/controllers/hardware/controller.go deleted file mode 100644 index 510d545dd..000000000 --- a/pkg/controllers/hardware/controller.go +++ /dev/null @@ -1,81 +0,0 @@ -package hardware - -import ( - "context" - "fmt" - "time" - - "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" - "github.com/tinkerbell/tink/pkg/controllers" - "k8s.io/apimachinery/pkg/api/equality" - "k8s.io/apimachinery/pkg/api/errors" - controllerruntime "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/manager" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -// Controller is a type for managing Hardwares. -type Controller struct { - kubeClient client.Client - nowFunc func() time.Time -} - -func NewController(kubeClient client.Client) *Controller { - return &Controller{ - kubeClient: kubeClient, - nowFunc: time.Now, - } -} - -// +kubebuilder:rbac:groups=tinkerbell.org,resources=hardware;hardware/status,verbs=get;list;watch;update;patch - -func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { - stored := &v1alpha1.Hardware{} - if err := c.kubeClient.Get(ctx, req.NamespacedName, stored); err != nil { - if errors.IsNotFound(err) { - return reconcile.Result{}, nil - } - return controllers.RetryIfError(ctx, err) - } - if !stored.DeletionTimestamp.IsZero() { - return reconcile.Result{}, nil - } - hw := stored.DeepCopy() - - resp := c.reconcileDiskData(ctx, hw) - - // Patch any changes, regardless of errors - if !equality.Semantic.DeepEqual(hw, stored) { - if perr := c.kubeClient.Patch(ctx, hw, client.MergeFrom(stored)); perr != nil { - return reconcile.Result{}, fmt.Errorf("error patching hardware %s, %w", hw.Name, perr) - } - } - - return resp, nil -} - -func (c *Controller) reconcileDiskData(_ context.Context, hardware *v1alpha1.Hardware) reconcile.Result { - if hardware.Spec.Disks == nil { - foundDisks := make([]v1alpha1.Disk, 0) - - if hardware.Spec.Metadata != nil && - hardware.Spec.Metadata.Instance != nil && - hardware.Spec.Metadata.Instance.Storage != nil { - for _, disk := range hardware.Spec.Metadata.Instance.Storage.Disks { - foundDisks = append(foundDisks, v1alpha1.Disk{Device: disk.Device}) - } - } - - hardware.Spec.Disks = foundDisks - } - - return reconcile.Result{} -} - -func (c *Controller) Register(_ context.Context, m manager.Manager) error { - return controllerruntime. - NewControllerManagedBy(m). - For(&v1alpha1.Hardware{}). - Complete(c) -} diff --git a/pkg/controllers/hardware/controller_test.go b/pkg/controllers/hardware/controller_test.go deleted file mode 100644 index b79456a44..000000000 --- a/pkg/controllers/hardware/controller_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package hardware - -import ( - "context" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -var runtimescheme = runtime.NewScheme() - -func init() { - _ = clientgoscheme.AddToScheme(runtimescheme) - _ = v1alpha1.AddToScheme(runtimescheme) -} - -func GetFakeClientBuilder() *fake.ClientBuilder { - return fake.NewClientBuilder().WithScheme( - runtimescheme, - ).WithRuntimeObjects( - &v1alpha1.Hardware{}, - ) -} - -func TestReconcile(t *testing.T) { - cases := []struct { - name string - seedHardware *v1alpha1.Hardware - req reconcile.Request - want reconcile.Result - wantHw *v1alpha1.Hardware - wantErr error - }{ - { - name: "ReconcileDiskData", - seedHardware: &v1alpha1.Hardware{ - TypeMeta: metav1.TypeMeta{ - Kind: "Hardware", - APIVersion: "tinkerbell.org/v1alpha1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "hardware1", - Namespace: "default", - }, - Spec: v1alpha1.HardwareSpec{ - Metadata: &v1alpha1.HardwareMetadata{ - Instance: &v1alpha1.MetadataInstance{ - Storage: &v1alpha1.MetadataInstanceStorage{ - Disks: []*v1alpha1.MetadataInstanceStorageDisk{ - { - Device: "/dev/sda", - }, - }, - }, - }, - }, - }, - }, - req: reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: "hardware1", - Namespace: "default", - }, - }, - want: reconcile.Result{}, - wantHw: &v1alpha1.Hardware{ - TypeMeta: metav1.TypeMeta{ - Kind: "Hardware", - APIVersion: "tinkerbell.org/v1alpha1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "hardware1", - Namespace: "default", - ResourceVersion: "1000", - }, - Spec: v1alpha1.HardwareSpec{ - Disks: []v1alpha1.Disk{ - { - Device: "/dev/sda", - }, - }, - Metadata: &v1alpha1.HardwareMetadata{ - Instance: &v1alpha1.MetadataInstance{ - Storage: &v1alpha1.MetadataInstanceStorage{ - Disks: []*v1alpha1.MetadataInstanceStorageDisk{ - { - Device: "/dev/sda", - }, - }, - }, - }, - }, - }, - }, - wantErr: nil, - }, - } - for _, tc := range cases { - kc := GetFakeClientBuilder() - if tc.seedHardware != nil { - kc = kc.WithObjects(tc.seedHardware) - } - - controller := &Controller{ - kubeClient: kc.Build(), - } - - t.Run(tc.name, func(t *testing.T) { - got, gotErr := controller.Reconcile(context.Background(), tc.req) - if gotErr != nil { - if tc.wantErr == nil { - t.Errorf(`Got unexpected error: %v"`, gotErr) - } else if gotErr.Error() != tc.wantErr.Error() { - t.Errorf(`Got unexpected error: got "%v" wanted "%v"`, gotErr, tc.wantErr) - } - return - } - if gotErr == nil && tc.wantErr != nil { - t.Errorf("Missing expected error: %v", tc.wantErr) - return - } - if tc.want != got { - t.Errorf("Got unexpected result. Wanted %v, got %v", tc.want, got) - // Don't return, also check the modified object - } - hwflow := &v1alpha1.Hardware{} - err := controller.kubeClient.Get( - context.Background(), - client.ObjectKey{Name: tc.wantHw.Name, Namespace: tc.wantHw.Namespace}, - hwflow) - if err != nil { - t.Errorf("Error finding desired hardware: %v", err) - return - } - - if diff := cmp.Diff(tc.wantHw, hwflow); diff != "" { - t.Errorf("unexpected difference:\n%v", diff) - } - }) - } -}