Skip to content

Commit

Permalink
multiclusterhub package after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Elai Shalev committed Jun 4, 2024
1 parent c2a8833 commit 86c3ccf
Show file tree
Hide file tree
Showing 13 changed files with 1,863 additions and 15 deletions.
10 changes: 4 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ linters-settings:
- "github.com/kelseyhightower/envconfig"
- "github.com/red-hat-storage/ocs-operator"
- "github.com/stmcginnis/gofish"
- "github.com/stolostron/multiclusterhub-operator/api/v1"
revive:
rules:
- name: indent-error-flow
Expand Down Expand Up @@ -124,13 +125,10 @@ output:
issues:
include:
- EXC0002 # disable excluding of issues about comments from golint
- EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments
exclude-rules:
#- # Exclude some linters from running on tests files.
- path: 'pkg/polarion'
- path: "pkg/polarion"
linters:
- exhaustive



7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/openshift/assisted-service/api v0.0.0
github.com/openshift/assisted-service/models v0.0.0
github.com/openshift/custom-resource-status v1.1.3-0.20220503160415-f2fdb4999d87
github.com/openshift/elasticsearch-operator v0.0.0-20220613183908-e1648e67c298
github.com/red-hat-storage/ocs-operator v0.4.13
github.com/stmcginnis/gofish v0.15.0
github.com/stolostron/multiclusterhub-operator v0.0.0-20220516144733-74b7bd46ac55
github.com/stretchr/testify v1.9.0
github.com/vmware-tanzu/velero v1.12.1
golang.org/x/crypto v0.23.0
Expand All @@ -73,6 +75,9 @@ require (
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
github.com/bshuster-repo/logrus-logstash-hook v1.0.2 // indirect
github.com/bugsnag/bugsnag-go v2.1.2+incompatible // indirect
github.com/bugsnag/panicwrap v1.3.4 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand Down Expand Up @@ -110,6 +115,7 @@ require (
github.com/go-openapi/validate v0.22.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-test/deep v1.1.0 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -165,7 +171,6 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/gomega v1.31.1 // indirect
github.com/openshift/assisted-service v1.0.10-0.20230830164851-6573b5d7021d // indirect
github.com/openshift/custom-resource-status v1.1.3-0.20220503160415-f2fdb4999d87 // indirect
github.com/openshift/library-go v0.0.0-20231027143522-b8cd45d2d2c8 // indirect
github.com/operator-framework/operator-registry v1.35.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
Expand Down
19 changes: 11 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,13 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1 h1:pgAtgj+A31JBVtEHu2uHuEx0n+2ukqUJnS2vVe5pQNA=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bugsnag/bugsnag-go v1.5.3 h1:yeRUT3mUE13jL1tGwvoQsKdVbAsQx9AJ+fqahKveP04=
github.com/bugsnag/bugsnag-go v1.5.3/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
github.com/bugsnag/panicwrap v1.2.0 h1:OzrKrRvXis8qEvOkfcxNcYbOd2O7xXS2nnKMEMABFQA=
github.com/bugsnag/panicwrap v1.2.0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bshuster-repo/logrus-logstash-hook v1.0.2 h1:JYRWo+QGnQdedgshosug9hxpPYTB9oJ1ZZD3fY31alU=
github.com/bshuster-repo/logrus-logstash-hook v1.0.2/go.mod h1:HgYntJprnHSPaF9VPPPLP1L5S1vMWxRfa1J+vzDrDTw=
github.com/bugsnag/bugsnag-go v2.1.2+incompatible h1:E7dor84qzwUO8KdCM68CZwq9QOSR7HXlLx3Wj5vui2s=
github.com/bugsnag/bugsnag-go v2.1.2+incompatible/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
github.com/bugsnag/panicwrap v1.3.4 h1:A6sXFtDGsgU/4BLf5JT0o5uYg3EeKgGx3Sfs+/uk3pU=
github.com/bugsnag/panicwrap v1.3.4/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
Expand Down Expand Up @@ -1005,8 +1006,8 @@ github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY9
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus v0.0.0-20181025153459-66d97aec3384/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84=
github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -1677,6 +1678,8 @@ github.com/stmcginnis/gofish v0.15.0 h1:8TG41+lvJk/0Nf8CIIYErxbMlQUy80W0JFRZP3Ld
github.com/stmcginnis/gofish v0.15.0/go.mod h1:BLDSFTp8pDlf/xDbLZa+F7f7eW0E/CHCboggsu8CznI=
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stolostron/multiclusterhub-operator v0.0.0-20220516144733-74b7bd46ac55 h1:7+GfRgTFlILbeLbnvCO/KAFhNn9RoWGnOcwL21f1nxI=
github.com/stolostron/multiclusterhub-operator v0.0.0-20220516144733-74b7bd46ac55/go.mod h1:MOs4J7cOEv55UQypbSNxshR9nEzyuR3ohRs7EVt4Ato=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
7 changes: 7 additions & 0 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
agentInstallV1Beta1 "github.com/openshift/assisted-service/api/v1beta1"
hiveV1 "github.com/openshift/hive/apis/hive/v1"
moduleV1Beta1 "github.com/rh-ecosystem-edge/kernel-module-management/api/v1beta1"
mchv1 "github.com/stolostron/multiclusterhub-operator/api/v1"
"k8s.io/client-go/kubernetes/scheme"
coreV1Client "k8s.io/client-go/kubernetes/typed/core/v1"
storageV1Client "k8s.io/client-go/kubernetes/typed/storage/v1"
Expand Down Expand Up @@ -269,6 +270,10 @@ func SetScheme(crScheme *runtime.Scheme) error {
return err
}

if err := mchv1.AddToScheme(crScheme); err != nil {
return err
}

if err := configV1.Install(crScheme); err != nil {
return err
}
Expand Down Expand Up @@ -514,6 +519,8 @@ func GetTestClients(tcp TestClientParams) *Settings {
genericClientObjects = append(genericClientObjects, v)
case *performanceV2.PerformanceProfile:
genericClientObjects = append(genericClientObjects, v)
case *mchv1.MultiClusterHub:
genericClientObjects = append(genericClientObjects, v)
case *tunedv1.Tuned:
genericClientObjects = append(genericClientObjects, v)
case *agentInstallV1Beta1.AgentServiceConfig:
Expand Down
219 changes: 219 additions & 0 deletions pkg/multiclusterhub/multiclusterhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package multiclusterhub

import (
"context"
"fmt"

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

mchv1 "github.com/stolostron/multiclusterhub-operator/api/v1"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// MultiClusterHubBuilder provides struct for the MultiClusterHub object containing connection to
// the cluster and the MultiClusterHub definitions.
type MultiClusterHubBuilder struct {
Definition *mchv1.MultiClusterHub
Object *mchv1.MultiClusterHub
errorMsg string
apiClient goclient.Client
}

// NewMultiClusterHubBuilder creates a new instance of MultiClusterHubBuilder.
func NewMultiClusterHubBuilder(apiClient *clients.Settings, name, namespace string) *MultiClusterHubBuilder {
glog.V(100).Infof(
`Initializing new MultiClusterHub structure with the following params: name: %s, namespace:`,
name, namespace)

if apiClient == nil {
glog.V(100).Infof("apiClient cannot be nil")

return nil
}

builder := MultiClusterHubBuilder{
apiClient: apiClient.Client,
Definition: &mchv1.MultiClusterHub{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
},
}

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

return nil
}

if namespace == "" {
glog.V(100).Infof("The namespace of the MultiClusterHub is empty")

return nil
}

return &builder
}

// PullMultiClusterHub loads an existing MultiClusterHub into MultiClusterHubBuilder struct.
func PullMultiClusterHub(apiClient *clients.Settings, name, namespace string) (*MultiClusterHubBuilder, error) {
glog.V(100).Infof("Pulling existing MultiClusterHub name: %s from namespace %s", name, namespace)

if apiClient == nil {
return nil, fmt.Errorf("apiClient cannot be nil")
}

builder := MultiClusterHubBuilder{
apiClient: apiClient.Client,
Definition: &mchv1.MultiClusterHub{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
},
}

if name == "" {
builder.errorMsg = "MultiClusterHub 'name' cannot be empty"

return &builder, nil
}

if namespace == "" {
builder.errorMsg = "MultiClusterHub 'namespace' cannot be empty"

return &builder, nil
}

if !builder.Exists() {
return nil, fmt.Errorf("MultiClusterHub object %s does not exist", name)
}

builder.Definition = builder.Object

return &builder, nil
}

// Get fetches the defined MultiClusterHub from the cluster.
func (builder *MultiClusterHubBuilder) Get() (*mchv1.MultiClusterHub, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof("Getting MultiClusterHub %s from namespace %s",
builder.Definition.Name, builder.Definition.Namespace)

multiClusterHub := &mchv1.MultiClusterHub{}
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{
Name: builder.Definition.Name,
Namespace: builder.Definition.Namespace,
}, multiClusterHub)

if err != nil {
return nil, err
}

return multiClusterHub, err
}

// Update modifies an existing MultiClusterHub on the cluster.
func (builder *MultiClusterHubBuilder) Update() (*MultiClusterHubBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}

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

exists := builder.Exists()
if !exists {
return builder, fmt.Errorf("MulticlusterHub object does not exist")
}

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

builder.Object = builder.Definition

return builder, err
}

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

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

if !builder.Exists() {
return nil
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)

if err != nil {
return fmt.Errorf("cannot delete MultiClusterHub: %w", err)
}

builder.Object = nil
builder.Definition.ResourceVersion = ""
builder.Definition.CreationTimestamp = metav1.Time{}

return nil
}

// Exists checks if the defined MultiClusterHub has already been created.
func (builder *MultiClusterHubBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

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

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

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

// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *MultiClusterHubBuilder) validate() (bool, error) {
resourceCRD := "MultiClusterHub"

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 undefined", resourceCRD)

builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
}

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

builder.errorMsg = fmt.Sprintf("%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 86c3ccf

Please sign in to comment.