blob: d4277efeeb34c99418112b0accdc7e89ffb0b7f2 [file] [log] [blame]
ktimoney3570d5a2022-05-24 13:54:55 +01001// -
2// ========================LICENSE_START=================================
3// O-RAN-SC
4// %%
5// Copyright (C) 2022: Nordix Foundation
6// %%
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18// ========================LICENSE_END===================================
19//
20
ktimoney6be05292022-03-02 12:53:14 +000021package main
22
23import (
24 "context"
25 "flag"
26 "fmt"
27 "github.com/Nerzal/gocloak/v10"
28 "helm.sh/helm/v3/pkg/action"
29 "helm.sh/helm/v3/pkg/cli"
30 "helm.sh/helm/v3/pkg/kube"
31 versioned "istio.io/client-go/pkg/clientset/versioned"
32 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33 "k8s.io/cli-runtime/pkg/genericclioptions"
34 "k8s.io/client-go/rest"
35 clientcmd "k8s.io/client-go/tools/clientcmd"
36 "os"
37 "path/filepath"
38)
39
40var settings *cli.EnvSettings
41var chartName string
42var releaseName string
43var namespace string
44
45func main() {
46 flag.StringVar(&chartName, "chartName", "hello-world", "Chart name")
47 flag.StringVar(&releaseName, "releaseName", "hello-world", "Release name")
48 flag.StringVar(&namespace, "namespace", "istio-nonrtric", "namespace for install")
49 flag.Parse()
50 settings = cli.New()
51
52 uninstallHelmChart(releaseName, namespace)
53 deleteIstio(namespace)
54 deleteClient()
55}
56
57func deleteIstio(namespace string) {
58 config, err := rest.InClusterConfig()
59 if err != nil {
60 // fallback to kubeconfig
61 home, exists := os.LookupEnv("HOME")
62 if !exists {
63 home = "/root"
64 }
65
66 kubeconfig := filepath.Join(home, ".kube", "config")
67 if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
68 kubeconfig = envvar
69 }
70 config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
71 if err != nil {
72 fmt.Println("failed to create K8s config")
73 }
74 }
75
76 ic, err := versioned.NewForConfig(config)
77 if err != nil {
78 fmt.Println("Failed to create istio client: %s", err)
79 }
80
81 gtClient := ic.NetworkingV1beta1().Gateways(namespace)
82 err = gtClient.Delete(context.TODO(), "nonrtric-istio-hw-gateway", metav1.DeleteOptions{})
83 if err != nil {
84 fmt.Println(err)
85 } else {
86 fmt.Println("Deleted Gateway nonrtric-istio-hw-gateway")
87 }
88
89 vsClient := ic.NetworkingV1beta1().VirtualServices(namespace)
90 err = vsClient.Delete(context.TODO(), "nonrtric-istio-hw-vs", metav1.DeleteOptions{})
91 if err != nil {
92 fmt.Println(err)
93 } else {
94 fmt.Println("Deleted VirtualServices nonrtric-istio-hw-vs")
95 }
96
97 apClient := ic.SecurityV1beta1().AuthorizationPolicies(namespace)
98 err = apClient.Delete(context.TODO(), "hw-policy", metav1.DeleteOptions{})
99 if err != nil {
100 fmt.Println(err)
101 } else {
102 fmt.Println("Deleted AuthorizationPolicy hw-policy")
103 }
104
105 raClient := ic.SecurityV1beta1().RequestAuthentications(namespace)
106 err = raClient.Delete(context.TODO(), "jwt-hw", metav1.DeleteOptions{})
107 if err != nil {
108 fmt.Println(err)
109 } else {
110 fmt.Println("Deleted RequestAuthentication jwt-hello-world")
111 }
112
113}
114
115func deleteClient() {
116 adminClient := gocloak.NewClient("http://192.168.49.2:31560")
117 ctx := context.Background()
118 token, err := adminClient.LoginAdmin(ctx, "admin", "admin", "master")
119 if err != nil {
120 fmt.Println(err)
121 }
122
123 clients, err := adminClient.GetClients(ctx, token.AccessToken, "hwrealm",
124 gocloak.GetClientsParams{
125 ClientID: gocloak.StringP("hwclient"),
126 },
127 )
128 if err != nil {
129 panic("List clients failed:" + err.Error())
130 }
131 for _, client := range clients {
132 err = adminClient.DeleteClient(ctx, token.AccessToken, "hwrealm", *client.ID)
133 if err != nil {
134 fmt.Println(err)
135 } else {
136 fmt.Println("Deleted client hwclient", *client.ID)
137 }
138 }
139
140}
141
142func uninstallHelmChart(name, ns string) {
143 actionConfig, err := getActionConfig(ns)
144 if err != nil {
145 fmt.Println(err)
146 }
147
148 iCli := action.NewUninstall(actionConfig)
149
150 //iCli.ReleaseName = name
151 resp, err := iCli.Run(name)
152 if err != nil {
153 fmt.Println(err)
154 }
155 fmt.Println("Successfully uninstalled release: ", resp.Release.Name)
156}
157
158func getActionConfig(namespace string) (*action.Configuration, error) {
159 actionConfig := new(action.Configuration)
160 // Create the rest config instance with ServiceAccount values loaded in them
161 config, err := rest.InClusterConfig()
162 if err != nil {
163 err = nil
164 // fallback to kubeconfig
165 home, exists := os.LookupEnv("HOME")
166 if !exists {
167 home = "/root"
168 }
169 kubeconfigPath := filepath.Join(home, ".kube", "config")
170 //kubeconfigPath := filepath.Join("~", ".kube", "config")
171 if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
172 kubeconfigPath = envvar
173 }
174 if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"),
175 func(format string, v ...interface{}) {
176 fmt.Sprintf(format, v)
177 }); err != nil {
178 //return actionConfig, err
179 fmt.Println(err)
180 }
181 } else {
182 // Create the ConfigFlags struct instance with initialized values from ServiceAccount
183 var kubeConfig *genericclioptions.ConfigFlags
184 kubeConfig = genericclioptions.NewConfigFlags(false)
185 kubeConfig.APIServer = &config.Host
186 kubeConfig.BearerToken = &config.BearerToken
187 kubeConfig.CAFile = &config.CAFile
188 kubeConfig.Namespace = &namespace
189 if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
190 fmt.Sprintf(format, v)
191 }); err != nil {
192 fmt.Println(err)
193 }
194 }
195 return actionConfig, err
196}