github.com/verrazzano/verrazzano@v1.7.1/tools/psr/backend/pkg/weblogic/domain.go (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package weblogic
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  	"k8s.io/apimachinery/pkg/runtime/schema"
    13  	"k8s.io/apimachinery/pkg/types"
    14  	"k8s.io/apimachinery/pkg/util/json"
    15  	"k8s.io/client-go/dynamic"
    16  )
    17  
    18  var (
    19  	specField      = "spec"
    20  	specReplicas   = []string{specField, "replicas"}
    21  	statusClusters = []string{"status", "clusters"}
    22  )
    23  
    24  // getScheme returns the WebLogic scheme needed to get unstructured data
    25  func getScheme() schema.GroupVersionResource {
    26  	return schema.GroupVersionResource{
    27  		Group:    "weblogic.oracle",
    28  		Version:  "v9",
    29  		Resource: "domains",
    30  	}
    31  }
    32  
    33  // GetReadyReplicas returns the readyReplicas from the first cluster in domain status
    34  func GetReadyReplicas(client dynamic.Interface, namespace string, name string) (int64, error) {
    35  	domain, err := client.Resource(getScheme()).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
    36  	if err != nil {
    37  		return 0, err
    38  	}
    39  	clusters, found, err := unstructured.NestedSlice(domain.Object, statusClusters...)
    40  	if err != nil {
    41  		return 0, err
    42  	}
    43  	if !found {
    44  		return 0, fmt.Errorf("Failed to get clusters %v", err)
    45  	}
    46  	readyReplicas := clusters[0].(map[string]interface{})["readyReplicas"]
    47  	if readyReplicas != nil {
    48  		return readyReplicas.(int64), nil
    49  	}
    50  	return 0, nil
    51  }
    52  
    53  // GetCurrentReplicas returns the replicas value from /spec/replicas
    54  func GetCurrentReplicas(client dynamic.Interface, namespace string, name string) (int64, error) {
    55  	specReplicas = []string{specField, "replicas"}
    56  	domain, err := client.Resource(getScheme()).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
    57  	if err != nil {
    58  		return 0, err
    59  	}
    60  	rep, found, err := unstructured.NestedInt64(domain.Object, specReplicas...)
    61  	if err != nil {
    62  		return 0, err
    63  	}
    64  	if !found {
    65  		return 0, fmt.Errorf("Failed to get replicas %v %v", specReplicas, err)
    66  	}
    67  	return rep, nil
    68  
    69  }
    70  
    71  // PatchReplicas patches the replicas at /spec/replicas
    72  func PatchReplicas(client dynamic.Interface, namespace string, name string, replicas int64) error {
    73  	// patchInt64Value specifies a patch operation for a int64.
    74  	type patchInt64Value struct {
    75  		Op    string `json:"op"`
    76  		Path  string `json:"path"`
    77  		Value int64  `json:"value"`
    78  	}
    79  	payload := []patchInt64Value{{
    80  		Op:    "replace",
    81  		Path:  "/spec/replicas",
    82  		Value: replicas,
    83  	}}
    84  	payloadBytes, _ := json.Marshal(payload)
    85  	_, err := client.Resource(getScheme()).Namespace(namespace).Patch(context.TODO(), name, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
    86  	if err != nil {
    87  		return err
    88  	}
    89  	return nil
    90  }