-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Saeid Askari <[email protected]>
- Loading branch information
1 parent
c44b642
commit e658d1a
Showing
12 changed files
with
1,112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...o/api/cluster/v1alpha1/0000_02_clusters.open-cluster-management.io_clusterclaims.crd.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
...luster/v1alpha1/0000_05_clusters.open-cluster-management.io_addonplacementscores.crd.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
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.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
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.
Oops, something went wrong.
Oops, something went wrong.