Skip to content

Commit

Permalink
ptp: add PtpConfig resource and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klaskosk committed Oct 11, 2024
1 parent 908c8aa commit 7de1de1
Show file tree
Hide file tree
Showing 188 changed files with 2,697 additions and 9,087 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/openshift-kni/eco-goinfra
go 1.23

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/semver/v3 v3.3.0
github.com/blang/semver/v4 v4.0.0
github.com/go-openapi/errors v0.20.3
github.com/go-openapi/strfmt v0.21.3
Expand All @@ -30,7 +30,7 @@ require (
github.com/openshift-kni/lifecycle-agent v0.0.0-20241002223755-1e32b456449f // release-4.17
github.com/openshift-kni/numaresources-operator v0.4.17-0rc0.0.20240903095616-58342d895734 // release-4.17
github.com/openshift/api v3.9.1-0.20191111211345-a27ff30ebf09+incompatible
github.com/openshift/client-go v0.0.0-20240523113335-452272e0496d
github.com/openshift/client-go v0.0.1
github.com/openshift/cluster-logging-operator v0.0.0-20241003210634-afb65cea19d1 // release-5.9
github.com/openshift/cluster-nfd-operator v0.0.0-20240902145504-f70d1389d329 // release-4.17
github.com/openshift/cluster-node-tuning-operator v0.0.0-20241002235338-d7086bb43aa2 // release-4.17
Expand Down Expand Up @@ -203,6 +203,8 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

require github.com/openshift/ptp-operator v0.0.0-20241008160821-983423165f34

replace (
github.com/imdario/mergo => github.com/imdario/mergo v0.3.16
github.com/k8snetworkplumbingwg/sriov-network-operator => github.com/openshift/sriov-network-operator v0.0.0-20241007120942-40fc75e827a1 // release-4.17
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down Expand Up @@ -1266,8 +1266,8 @@ github.com/openshift-kni/numaresources-operator v0.4.17-0rc0.0.20240903095616-58
github.com/openshift-kni/numaresources-operator v0.4.17-0rc0.0.20240903095616-58342d895734/go.mod h1:3JEkNx0nBloibpk6cxx5NDz3qPRRgJ03fq23plPEkAI=
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6Beb1gQ96Ptej9AE/BvwCBiRj1E=
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
github.com/openshift/client-go v0.0.0-20240523113335-452272e0496d h1:Dq21KMlDTVy2fCIyp0gsW+6ir6FwD3RjnCuza2/bIyM=
github.com/openshift/client-go v0.0.0-20240523113335-452272e0496d/go.mod h1:jt2Q+6Iheyh6omSPkRMOC6Doad5My/FfBfJpATPD4g0=
github.com/openshift/client-go v0.0.1 h1:zJ9NsS9rwBtYkYzLCUECkdmrM6jPit3W7Q0+Pxf5gd4=
github.com/openshift/client-go v0.0.1/go.mod h1:I8qTI1lgErsWc6CVukSjP1PYqpafE7fue0ZPy7A2jiw=
github.com/openshift/cluster-logging-operator v0.0.0-20241003210634-afb65cea19d1 h1:wHc3FvG1CTkAfOQ6GoAahRc+TDgDapEwnn77zssj3Ac=
github.com/openshift/cluster-logging-operator v0.0.0-20241003210634-afb65cea19d1/go.mod h1:iLuWnS7rFs5XMY8Y5yZKFwLBWeo/7n7ts1kxJe3pElI=
github.com/openshift/cluster-nfd-operator v0.0.0-20240902145504-f70d1389d329 h1:FT5pUXcRFPEFQwTjQfGkPPn3OtV/OMjE4LVSnnkryjE=
Expand All @@ -1284,6 +1284,8 @@ github.com/openshift/local-storage-operator v0.0.0-20240712213037-f7a4384c92bb h
github.com/openshift/local-storage-operator v0.0.0-20240712213037-f7a4384c92bb/go.mod h1:Xd0acgr/PW7GbqjF/vM9cK0h+0XZyQgZj5w4GJ3GKoU=
github.com/openshift/machine-config-operator v0.0.1-0.20231024085435-7e1fb719c1ba h1:WM6K+m2xMAwbQDetKGhV/Rd8yukF3AsU1z74cqoWrz0=
github.com/openshift/machine-config-operator v0.0.1-0.20231024085435-7e1fb719c1ba/go.mod h1:mSt3ACow31pa1hTRONn+yT5e+KFkgi7G2bFEx5Nj+n0=
github.com/openshift/ptp-operator v0.0.0-20241008160821-983423165f34 h1:bqOw2RZDyEEsyS4gZNLnUbN88RJE9DiksdS06VGj/iY=
github.com/openshift/ptp-operator v0.0.0-20241008160821-983423165f34/go.mod h1:p05fjTh1GbWY8UXtutLOvy8m0MefN8kV1hOxoIzUeBE=
github.com/openshift/sriov-network-operator v0.0.0-20241007120942-40fc75e827a1 h1:eOONPWetpAKkcVjoki9HhhSkLpe47sxuKh/uAaUgInw=
github.com/openshift/sriov-network-operator v0.0.0-20241007120942-40fc75e827a1/go.mod h1:YL5Y2SIHqmR52FXrt4JyIaodWJyOnEK5n7PW4bL320Q=
github.com/ovn-org/ovn-kubernetes/go-controller v0.0.0-20240614144800-61d5c5937bd4 h1:rqCh/Kh/7ityDsd1tIh1EjLxJ7ie7J8pH9/nxLZURWc=
Expand Down
1 change: 0 additions & 1 deletion pkg/ptp/ptp.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/ptp/ptp_test.go

This file was deleted.

290 changes: 290 additions & 0 deletions pkg/ptp/ptpconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
package ptp

import (
"context"
"fmt"

goclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
ptpv1 "github.com/openshift/ptp-operator/api/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// PtpConfigBuilder provides a struct for the PtpConfig resource containing a connection to the cluster and the
// PtpConfig definition.
type PtpConfigBuilder struct {
// Definition of the PtpConfig used to create the object.
Definition *ptpv1.PtpConfig
// Object of the PtpConfig as it is on the cluster.
Object *ptpv1.PtpConfig
apiClient goclient.Client
errorMsg string
}

// NewPtpConfigBuilder creates a new instance of a PtpConfig builder.
func NewPtpConfigBuilder(apiClient *clients.Settings, name, nsname string) *PtpConfigBuilder {
glog.V(100).Infof("Initializing new PtpConfig structure with the following params: name: %s, nsname: %s", name, nsname)

if apiClient == nil {
glog.V(100).Info("The apiClient of the PtpConfig is nil")

return nil
}

err := apiClient.AttachScheme(ptpv1.AddToScheme)
if err != nil {
glog.V(100).Info("Failed to add ptp v1 scheme to client schemes")

return nil
}

builder := &PtpConfigBuilder{
apiClient: apiClient.Client,
Definition: &ptpv1.PtpConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}

if name == "" {
glog.V(100).Info("The name of the PtpConfig is empty")

builder.errorMsg = "ptpConfig 'name' cannot be empty"

return builder
}

if nsname == "" {
glog.V(100).Info("The namespace of the PtpConfig is empty")

builder.errorMsg = "ptpConfig 'nsname' cannot be empty"

return builder
}

return builder
}

// PullPtpConfig pulls an existing PtpConfig into a Builder struct.
func PullPtpConfig(apiClient *clients.Settings, name, nsname string) (*PtpConfigBuilder, error) {
glog.V(100).Infof("Pulling existing PtpConfig %s under namespace %s from cluster", name, nsname)

if apiClient == nil {
glog.V(100).Info("The apiClient is empty")

return nil, fmt.Errorf("ptpConfig 'apiClient' cannot be nil")
}

err := apiClient.AttachScheme(ptpv1.AddToScheme)
if err != nil {
glog.V(100).Info("Failed to add PtpConfig scheme to client schemes")

return nil, err
}

builder := &PtpConfigBuilder{
apiClient: apiClient.Client,
Definition: &ptpv1.PtpConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}

if name == "" {
glog.V(100).Info("The name of the PtpConfig is empty")

return nil, fmt.Errorf("ptpConfig 'name' cannot be empty")
}

if nsname == "" {
glog.V(100).Info("The namespace of the PtpConfig is empty")

return nil, fmt.Errorf("ptpConfig 'nsname' cannot be empty")
}

if !builder.Exists() {
glog.V(100).Info("The PtpConfig %s does not exist in namespace %s", name, nsname)

return nil, fmt.Errorf("ptpConfig object %s does not exist in namespace %s", name, nsname)
}

builder.Definition = builder.Object

return builder, nil
}

// Get returns the PtpConfig object if found.
func (builder *PtpConfigBuilder) Get() (*ptpv1.PtpConfig, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof(
"Getting PtpConfig object %s in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

ptpConfig := &ptpv1.PtpConfig{}
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{
Name: builder.Definition.Name,
Namespace: builder.Definition.Namespace,
}, ptpConfig)

if err != nil {
glog.V(100).Infof(
"PtpConfig object %s does not exist in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)

return nil, err
}

return ptpConfig, nil
}

// Exists checks whether the given PtpConfig exists on the cluster.
func (builder *PtpConfigBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

glog.V(100).Infof(
"Checking if PtpConfig %s exists in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

var err error
builder.Object, err = builder.Get()

return err == nil || !k8serrors.IsNotFound(err)
}

// Create makes a PtpConfig on the cluster if it does not already exist.
func (builder *PtpConfigBuilder) Create() (*PtpConfigBuilder, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof(
"Creating PtpConfig %s in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

if builder.Exists() {
return builder, nil
}

err := builder.apiClient.Create(context.TODO(), builder.Definition)
if err != nil {
return nil, err
}

builder.Object = builder.Definition

return builder, nil
}

// Update changes the existing PtpConfig resource on the cluster, falling back to deleting and recreating if the update
// fails when force is set.
func (builder *PtpConfigBuilder) Update(force bool) (*PtpConfigBuilder, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof(
"Updating PtpConfig %s in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
glog.V(100).Infof(
"PtpConfig %s does not exist in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

return nil, fmt.Errorf("cannot update non-existent ptpConfig")
}

builder.Definition.ResourceVersion = builder.Object.ResourceVersion

err := builder.apiClient.Update(context.TODO(), builder.Definition)
if err != nil {
if force {
glog.V(100).Infof(msg.FailToUpdateNotification("ptpConfig", builder.Definition.Name))

err := builder.Delete()
builder.Definition.ResourceVersion = ""

if err != nil {
glog.V(100).Infof(msg.FailToUpdateError("ptpConfig", builder.Definition.Name))

return nil, err
}

return builder.Create()
}

return nil, err
}

builder.Object = builder.Definition

return builder, nil
}

// Delete removes a PtpConfig from the cluster if it exists.
func (builder *PtpConfigBuilder) Delete() error {
if valid, err := builder.validate(); !valid {
return err
}

glog.V(100).Infof(
"Deleting PtpConfig %s in namespace %s", builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
glog.V(100).Infof(
"PtpConfig %s in namespace %s does not exist",
builder.Definition.Name, builder.Definition.Namespace)

builder.Object = nil

return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Object)
if err != nil {
return err
}

builder.Object = nil

return nil
}

// validate checks that the builder, definition, and apiClient are properly initialized and there is no errorMsg.
func (builder *PtpConfigBuilder) validate() (bool, error) {
resourceCRD := "ptpConfig"

if builder == nil {
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD)

return false, fmt.Errorf("error: received nil %s builder", resourceCRD)
}

if builder.Definition == nil {
glog.V(100).Infof("The %s is uninitialized", resourceCRD)

return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}

if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiClient is nil", resourceCRD)

return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}

if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message %s", resourceCRD, builder.errorMsg)

return false, fmt.Errorf(builder.errorMsg)
}

return true, nil
}
Loading

0 comments on commit 7de1de1

Please sign in to comment.