Skip to content

Commit

Permalink
fix getting cluster ID, add logs
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Mange <[email protected]>
  • Loading branch information
thibaultmg committed Feb 3, 2025
1 parent 0052f42 commit 839daae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/pkg/tests/observability_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ var _ = Describe("Observability:", func() {
watchDogRuleKustomizationPath := "../../../examples/alerts/watchdog_rule"
yamlB, err := kustomize.Render(kustomize.Options{KustomizationPath: watchDogRuleKustomizationPath})
Expect(err).NotTo(HaveOccurred())
klog.Infof("List of cluster IDs to install the watchdog alert: %s", expectedKSClusterNames)
for _, ks := range expectedKSClusterNames {
for idx, mc := range testOptions.ManagedClusters {
if mc.Name == ks {
Expand Down
22 changes: 22 additions & 0 deletions tests/pkg/utils/kube_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
)
Expand Down Expand Up @@ -48,6 +49,7 @@ func LogFailingTestStandardDebugInfo(opt TestOptions) {
CheckPodsInNamespace(hubClient, MCO_NAMESPACE, []string{}, map[string]string{})
printConfigMapsInNamespace(hubClient, MCO_NAMESPACE)
printSecretsInNamespace(hubClient, MCO_NAMESPACE)
LogManagedClusters(hubDynClient)

for _, mc := range opt.ManagedClusters {
if mc.Name == "local-cluster" {
Expand Down Expand Up @@ -302,6 +304,26 @@ func LogObjectEvents(client kubernetes.Interface, ns string, kind string, name s
klog.V(1).Infof("%s %q events: \n%s", kind, name, formattedEvents)
}

func LogManagedClusters(client dynamic.Interface) {
objs, err := client.Resource(NewOCMManagedClustersGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
klog.Errorf("failed to get ManagedClusters: %v", err)
return
}

var managedClustersData strings.Builder
managedClustersData.WriteString(">>>>>>>>>> Managed Clusters >>>>>>>>>>\n")
for _, obj := range objs.Items {
mc, err := json.MarshalIndent(obj, "", " ")
if err != nil {
klog.Errorf("Failed to marshal ManagedCluster %q: %s", obj.GetName(), err.Error())
}
managedClustersData.WriteString(string(mc))
}
managedClustersData.WriteString("<<<<<<<<<< Managed Clusters <<<<<<<<<<")
klog.Info(managedClustersData.String())
}

func printPodsStatuses(pods []corev1.Pod) {
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(writer, "NAME\tSTATUS\tRESTARTS\tAGE")
Expand Down
40 changes: 28 additions & 12 deletions tests/pkg/utils/mco_managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import (
"os"
"slices"

"github.com/onsi/ginkgo"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "open-cluster-management.io/api/cluster/v1"
)

const availableManagedClusterCondition = "ManagedClusterConditionAvailable"
const (
availableManagedClusterCondition = "ManagedClusterConditionAvailable"
idClusterClaim = "id.k8s.io"
)

var openshiftLabelSelector = labels.SelectorFromValidatedSet(map[string]string{
"vendor": "OpenShift",
})

func UpdateObservabilityFromManagedCluster(opt TestOptions, enableObservability bool) error {
clusterName := GetManagedClusterName(opt)
Expand Down Expand Up @@ -103,7 +112,7 @@ func ListManagedClusters(opt TestOptions) ([]string, error) {
}

func ListAvailableOCPManagedClusterIDs(opt TestOptions) ([]string, error) {
managedClusters, err := getManagedClusters(opt)
managedClusters, err := GetManagedClusters(opt)
if err != nil {
return nil, err
}
Expand All @@ -116,16 +125,14 @@ func ListAvailableOCPManagedClusterIDs(opt TestOptions) ([]string, error) {

ret := make([]string, 0, len(managedClusters))
for _, mc := range managedClusters {
if clusterID, ok := mc.ObjectMeta.Labels["clusterID"]; ok {
ret = append(ret, clusterID)
}
ret = append(ret, getManagedClusterID(mc))
}

return ret, nil
}

func ListAvailableKSManagedClusterNames(opt TestOptions) ([]string, error) {
managedClusters, err := getManagedClusters(opt)
managedClusters, err := GetManagedClusters(opt)
if err != nil {
return nil, err
}
Expand All @@ -138,20 +145,29 @@ func ListAvailableKSManagedClusterNames(opt TestOptions) ([]string, error) {

ret := make([]string, 0, len(managedClusters))
for _, mc := range managedClusters {
if clusterID, ok := mc.ObjectMeta.Labels["name"]; ok {
ret = append(ret, clusterID)
}
ret = append(ret, getManagedClusterID(mc))
}

return ret, nil
}

func isOpenshiftVendor(mc *clusterv1.ManagedCluster) bool {
vendor, ok := mc.ObjectMeta.Labels["vendor"]
return !ok || vendor != "OpenShift"
return openshiftLabelSelector.Matches(labels.Set(mc.GetLabels()))
}

func getManagedClusterID(mc *clusterv1.ManagedCluster) string {
for _, cc := range mc.Status.ClusterClaims {
if cc.Name == idClusterClaim {
return cc.Value
}
}

ginkgo.Fail(fmt.Sprintf("failed to get the managedCluster %q ID", mc.Name))

return ""
}

func getManagedClusters(opt TestOptions) ([]*clusterv1.ManagedCluster, error) {
func GetManagedClusters(opt TestOptions) ([]*clusterv1.ManagedCluster, error) {
clientDynamic := GetKubeClientDynamic(opt, true)
objs, err := clientDynamic.Resource(NewOCMManagedClustersGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
Expand Down

0 comments on commit 839daae

Please sign in to comment.