Skip to content

Commit 864c48c

Browse files
Add ClusterClaim for IBU statuses
Signed-off-by: Saeid Askari <saskari@redhat.com>
1 parent 8292c9a commit 864c48c

12 files changed

+1112
-0
lines changed

internal/common/clusterclaims.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/go-logr/logr"
9+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10+
apierrors "k8s.io/apimachinery/pkg/api/errors"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
13+
clusterv1 "open-cluster-management.io/api/cluster/v1alpha1"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
15+
)
16+
17+
const ClusterClaimPrefix = "lcm.openshift.io/ibu-"
18+
19+
func isClusterClaimCRDInstalled(ctx context.Context, client client.Client) (bool, error) {
20+
crd := &apiextensionsv1.CustomResourceDefinition{}
21+
if err := client.Get(ctx, types.NamespacedName{
22+
Name: "clusterclaims.cluster.open-cluster-management.io",
23+
}, crd); err != nil {
24+
if apierrors.IsNotFound(err) {
25+
return false, nil
26+
}
27+
return false, fmt.Errorf("could not get ClusterClaim crd: %w", err)
28+
}
29+
return true, nil
30+
}
31+
32+
func CleanupClusterClaims(ctx context.Context, client client.Client, log *logr.Logger) error {
33+
if installed, err := isClusterClaimCRDInstalled(ctx, client); err != nil {
34+
return err
35+
} else if !installed {
36+
log.Info("ClusterClaim CRD not found; Skip creating ClusterClaim")
37+
return nil
38+
}
39+
cl := &clusterv1.ClusterClaimList{}
40+
err := client.List(ctx, cl)
41+
if err != nil {
42+
return fmt.Errorf("failed to list ClusterClaims: %w", err)
43+
}
44+
for _, cc := range cl.Items {
45+
if strings.HasPrefix(cc.Name, ClusterClaimPrefix) {
46+
err := client.Delete(ctx, &cc)
47+
if err != nil {
48+
return fmt.Errorf("failed to delete ClusterClaim: %w", err)
49+
}
50+
}
51+
}
52+
return nil
53+
}
54+
55+
func CreateClusterClaim(ctx context.Context, client client.Client, log *logr.Logger, stage string, status string) error {
56+
if installed, err := isClusterClaimCRDInstalled(ctx, client); err != nil {
57+
return err
58+
} else if !installed {
59+
log.Info("ClusterClaim CRD not found; Skip creating ClusterClaim")
60+
return nil
61+
}
62+
cc := &clusterv1.ClusterClaim{
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: ClusterClaimPrefix + stage,
65+
},
66+
Spec: clusterv1.ClusterClaimSpec{
67+
Value: status,
68+
},
69+
}
70+
err := client.Create(ctx, cc)
71+
if apierrors.IsAlreadyExists(err) {
72+
log.Info("ClusterClaim already exists")
73+
} else if err != nil {
74+
return fmt.Errorf("could not create ClusterClaim: %w", err)
75+
}
76+
return nil
77+
}

internal/common/consts.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ var CertPrefixes = []string{
155155
}
156156

157157
var TarOpts = []string{"--selinux", "--xattrs", "--xattrs-include=*", "--acls"}
158+
159+
// status for ClusterClaims
160+
const (
161+
Failed = "failed"
162+
Completed = "completed"
163+
)
164+
165+
// actions for ClusterClaims
166+
const (
167+
Prep = "prep"
168+
Upgrade = "upgrade"
169+
Finalize = "finalize"
170+
Abort = "abort"
171+
)

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/cluster/v1alpha1/0000_02_clusters.open-cluster-management.io_clusterclaims.crd.yaml

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/cluster/v1alpha1/0000_05_clusters.open-cluster-management.io_addonplacementscores.crd.yaml

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/cluster/v1alpha1/doc.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/cluster/v1alpha1/register.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)