Skip to content

Commit

Permalink
Add ClusterClaim for IBU statuses
Browse files Browse the repository at this point in the history
Signed-off-by: Saeid Askari <[email protected]>
  • Loading branch information
sudomakeinstall2 committed Sep 10, 2024
1 parent c44b642 commit e658d1a
Show file tree
Hide file tree
Showing 12 changed files with 1,112 additions and 0 deletions.
77 changes: 77 additions & 0 deletions internal/common/clusterclaims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package common

import (
"context"
"fmt"
"strings"

"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "open-cluster-management.io/api/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const ClusterClaimPrefix = "lcm.openshift.io/ibu-"

func isClusterClaimCRDInstalled(ctx context.Context, client client.Client) (bool, error) {
crd := &apiextensionsv1.CustomResourceDefinition{}
if err := client.Get(ctx, types.NamespacedName{
Name: "clusterclaims.cluster.open-cluster-management.io",
}, crd); err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("could not get ClusterClaim crd: %w", err)
}
return true, nil
}

func CleanupClusterClaims(ctx context.Context, client client.Client, log *logr.Logger) error {
if installed, err := isClusterClaimCRDInstalled(ctx, client); err != nil {
return err
} else if !installed {
log.Info("ClusterClaim CRD not found; Skip creating ClusterClaim")
return nil
}
cl := &clusterv1.ClusterClaimList{}
err := client.List(ctx, cl)
if err != nil {
return fmt.Errorf("failed to list ClusterClaims: %w", err)
}
for _, cc := range cl.Items {
if strings.HasPrefix(cc.Name, ClusterClaimPrefix) {
err := client.Delete(ctx, &cc)
if err != nil {
return fmt.Errorf("failed to delete ClusterClaim: %w", err)
}
}
}
return nil
}

func CreateClusterClaim(ctx context.Context, client client.Client, log *logr.Logger, stage string, status string) error {
if installed, err := isClusterClaimCRDInstalled(ctx, client); err != nil {
return err
} else if !installed {
log.Info("ClusterClaim CRD not found; Skip creating ClusterClaim")
return nil
}
cc := &clusterv1.ClusterClaim{
ObjectMeta: metav1.ObjectMeta{
Name: ClusterClaimPrefix + stage,
},
Spec: clusterv1.ClusterClaimSpec{
Value: status,
},
}
err := client.Create(ctx, cc)
if apierrors.IsAlreadyExists(err) {
log.Info("ClusterClaim already exists")
} else if err != nil {
return fmt.Errorf("could not create ClusterClaim: %w", err)
}
return nil
}
14 changes: 14 additions & 0 deletions internal/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,17 @@ var CertPrefixes = []string{
}

var TarOpts = []string{"--selinux", "--xattrs", "--xattrs-include=*", "--acls"}

// status for ClusterClaims
const (
Failed = "failed"
Completed = "completed"
)

// actions for ClusterClaims
const (
Prep = "prep"
Upgrade = "upgrade"
Finalize = "finalize"
Abort = "abort"
)
1 change: 1 addition & 0 deletions vendor/modules.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/open-cluster-management.io/api/cluster/v1alpha1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/open-cluster-management.io/api/cluster/v1alpha1/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e658d1a

Please sign in to comment.