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