Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add ClusterClaim for IBU statuses #628

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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