|
| 1 | +package kiali |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// IstioConfig calls the Kiali Istio config API to get all Istio objects in the mesh. |
| 12 | +// Returns the full YAML resources and additional details about each object. |
| 13 | +func (k *Kiali) IstioConfig(ctx context.Context) (string, error) { |
| 14 | + endpoint := IstioConfigEndpoint + "?validate=true" |
| 15 | + |
| 16 | + return k.executeRequest(ctx, http.MethodGet, endpoint, "", nil) |
| 17 | +} |
| 18 | + |
| 19 | +// IstioObjectDetails returns detailed information about a specific Istio object. |
| 20 | +// Parameters: |
| 21 | +// - namespace: the namespace containing the Istio object |
| 22 | +// - group: the API group (e.g., "networking.istio.io", "gateway.networking.k8s.io") |
| 23 | +// - version: the API version (e.g., "v1", "v1beta1") |
| 24 | +// - kind: the resource kind (e.g., "DestinationRule", "VirtualService", "HTTPRoute") |
| 25 | +// - name: the name of the resource |
| 26 | +func (k *Kiali) IstioObjectDetails(ctx context.Context, namespace, group, version, kind, name string) (string, error) { |
| 27 | + if namespace == "" { |
| 28 | + return "", fmt.Errorf("namespace is required") |
| 29 | + } |
| 30 | + if group == "" { |
| 31 | + return "", fmt.Errorf("group is required") |
| 32 | + } |
| 33 | + if version == "" { |
| 34 | + return "", fmt.Errorf("version is required") |
| 35 | + } |
| 36 | + if kind == "" { |
| 37 | + return "", fmt.Errorf("kind is required") |
| 38 | + } |
| 39 | + if name == "" { |
| 40 | + return "", fmt.Errorf("name is required") |
| 41 | + } |
| 42 | + endpoint := fmt.Sprintf(IstioObjectEndpoint+"?validate=true&help=true", |
| 43 | + url.PathEscape(namespace), |
| 44 | + url.PathEscape(group), |
| 45 | + url.PathEscape(version), |
| 46 | + url.PathEscape(kind), |
| 47 | + url.PathEscape(name)) |
| 48 | + |
| 49 | + return k.executeRequest(ctx, http.MethodGet, endpoint, "", nil) |
| 50 | +} |
| 51 | + |
| 52 | +// IstioObjectPatch patches an existing Istio object using PATCH method. |
| 53 | +// Parameters: |
| 54 | +// - namespace: the namespace containing the Istio object |
| 55 | +// - group: the API group (e.g., "networking.istio.io", "gateway.networking.k8s.io") |
| 56 | +// - version: the API version (e.g., "v1", "v1beta1") |
| 57 | +// - kind: the resource kind (e.g., "DestinationRule", "VirtualService", "HTTPRoute") |
| 58 | +// - name: the name of the resource |
| 59 | +// - jsonPatch: the JSON patch data to apply |
| 60 | +func (k *Kiali) IstioObjectPatch(ctx context.Context, namespace, group, version, kind, name, jsonPatch string) (string, error) { |
| 61 | + if namespace == "" { |
| 62 | + return "", fmt.Errorf("namespace is required") |
| 63 | + } |
| 64 | + if group == "" { |
| 65 | + return "", fmt.Errorf("group is required") |
| 66 | + } |
| 67 | + if version == "" { |
| 68 | + return "", fmt.Errorf("version is required") |
| 69 | + } |
| 70 | + if kind == "" { |
| 71 | + return "", fmt.Errorf("kind is required") |
| 72 | + } |
| 73 | + if name == "" { |
| 74 | + return "", fmt.Errorf("name is required") |
| 75 | + } |
| 76 | + if jsonPatch == "" { |
| 77 | + return "", fmt.Errorf("json patch data is required") |
| 78 | + } |
| 79 | + endpoint := fmt.Sprintf(IstioObjectEndpoint, |
| 80 | + url.PathEscape(namespace), |
| 81 | + url.PathEscape(group), |
| 82 | + url.PathEscape(version), |
| 83 | + url.PathEscape(kind), |
| 84 | + url.PathEscape(name)) |
| 85 | + |
| 86 | + return k.executeRequest(ctx, http.MethodPatch, endpoint, "application/json", strings.NewReader(jsonPatch)) |
| 87 | +} |
| 88 | + |
| 89 | +// IstioObjectCreate creates a new Istio object using POST method. |
| 90 | +// Parameters: |
| 91 | +// - namespace: the namespace where the Istio object will be created |
| 92 | +// - group: the API group (e.g., "networking.istio.io", "gateway.networking.k8s.io") |
| 93 | +// - version: the API version (e.g., "v1", "v1beta1") |
| 94 | +// - kind: the resource kind (e.g., "DestinationRule", "VirtualService", "HTTPRoute") |
| 95 | +// - jsonData: the JSON data for the new object |
| 96 | +func (k *Kiali) IstioObjectCreate(ctx context.Context, namespace, group, version, kind, jsonData string) (string, error) { |
| 97 | + if namespace == "" { |
| 98 | + return "", fmt.Errorf("namespace is required") |
| 99 | + } |
| 100 | + if group == "" { |
| 101 | + return "", fmt.Errorf("group is required") |
| 102 | + } |
| 103 | + if version == "" { |
| 104 | + return "", fmt.Errorf("version is required") |
| 105 | + } |
| 106 | + if kind == "" { |
| 107 | + return "", fmt.Errorf("kind is required") |
| 108 | + } |
| 109 | + if jsonData == "" { |
| 110 | + return "", fmt.Errorf("json data is required") |
| 111 | + } |
| 112 | + endpoint := fmt.Sprintf(IstioObjectCreateEndpoint, |
| 113 | + url.PathEscape(namespace), |
| 114 | + url.PathEscape(group), |
| 115 | + url.PathEscape(version), |
| 116 | + url.PathEscape(kind)) |
| 117 | + |
| 118 | + return k.executeRequest(ctx, http.MethodPost, endpoint, "application/json", strings.NewReader(jsonData)) |
| 119 | +} |
| 120 | + |
| 121 | +// IstioObjectDelete deletes an existing Istio object using DELETE method. |
| 122 | +// Parameters: |
| 123 | +// - namespace: the namespace containing the Istio object |
| 124 | +// - group: the API group (e.g., "networking.istio.io", "gateway.networking.k8s.io") |
| 125 | +// - version: the API version (e.g., "v1", "v1beta1") |
| 126 | +// - kind: the resource kind (e.g., "DestinationRule", "VirtualService", "HTTPRoute", "Gateway") |
| 127 | +// - name: the name of the resource |
| 128 | +func (k *Kiali) IstioObjectDelete(ctx context.Context, namespace, group, version, kind, name string) (string, error) { |
| 129 | + if namespace == "" { |
| 130 | + return "", fmt.Errorf("namespace is required") |
| 131 | + } |
| 132 | + if group == "" { |
| 133 | + return "", fmt.Errorf("group is required") |
| 134 | + } |
| 135 | + if version == "" { |
| 136 | + return "", fmt.Errorf("version is required") |
| 137 | + } |
| 138 | + if kind == "" { |
| 139 | + return "", fmt.Errorf("kind is required") |
| 140 | + } |
| 141 | + if name == "" { |
| 142 | + return "", fmt.Errorf("name is required") |
| 143 | + } |
| 144 | + endpoint := fmt.Sprintf(IstioObjectEndpoint, |
| 145 | + url.PathEscape(namespace), |
| 146 | + url.PathEscape(group), |
| 147 | + url.PathEscape(version), |
| 148 | + url.PathEscape(kind), |
| 149 | + url.PathEscape(name)) |
| 150 | + |
| 151 | + return k.executeRequest(ctx, http.MethodDelete, endpoint, "", nil) |
| 152 | +} |
0 commit comments