github.com/kyma-project/kyma-environment-broker@v0.0.1/cmd/subscriptioncleanup/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "net/url" 9 10 "github.com/kyma-project/kyma-environment-broker/cmd/subscriptioncleanup/cloudprovider" 11 "github.com/kyma-project/kyma-environment-broker/common/gardener" 12 13 "github.com/kyma-project/kyma-environment-broker/cmd/subscriptioncleanup/job" 14 log "github.com/sirupsen/logrus" 15 "github.com/vrischmann/envconfig" 16 "k8s.io/client-go/dynamic" 17 "k8s.io/client-go/kubernetes" 18 restclient "k8s.io/client-go/rest" 19 "k8s.io/client-go/tools/clientcmd" 20 ) 21 22 type config struct { 23 Gardener struct { 24 KubeconfigPath string `envconfig:"default=/gardener/kubeconfig"` 25 Project string `envconfig:"default="` 26 } 27 } 28 29 func main() { 30 log.Info("Starting cleanup job!") 31 cfg := config{} 32 err := envconfig.InitWithPrefix(&cfg, "APP") 33 exitOnError(err, "Failed to load application config") 34 35 clusterConfig, err := newClusterConfig(cfg) 36 exitOnError(err, "Failed to create kubernetes cluster client") 37 38 kubernetesInterface, err := newKubernetesInterface(clusterConfig) 39 exitOnError(err, "Failed to create kubernetes client") 40 41 gardenerClient, err := dynamic.NewForConfig(clusterConfig) 42 exitOnError(err, "Failed to create kubernetes client") 43 44 gardenerNamespace := fmt.Sprintf("garden-%s", cfg.Gardener.Project) 45 shootInterface := gardenerClient.Resource(gardener.ShootResource).Namespace(gardenerNamespace) 46 secretBindingsInterface := gardenerClient.Resource(gardener.SecretBindingResource).Namespace(gardenerNamespace) 47 48 cleaner := job.NewCleaner(context.Background(), kubernetesInterface, secretBindingsInterface, shootInterface, cloudprovider.NewProviderFactory()) 49 err = cleaner.Do() 50 HaltIstioSidecar() 51 52 exitOnError(err, "Job execution failed") 53 54 log.Info("Cleanup job finished successfully!") 55 } 56 57 func exitOnError(err error, context string) { 58 if err != nil { 59 log.Fatal(fmt.Errorf("%s: %s", context, err)) 60 } 61 } 62 63 func HaltIstioSidecar() { 64 log.Info("# HALT ISTIO SIDECAR #") 65 resp, err := http.PostForm("http://127.0.0.1:15020/quitquitquit", url.Values{}) 66 67 if err != nil { 68 log.Errorf("unable to send post request to quit Istio sidecar: %s", err) 69 return 70 } 71 72 if resp.StatusCode >= 200 && resp.StatusCode <= 299 { 73 log.Infof("Quiting istio, response status is: %d", resp.StatusCode) 74 return 75 } 76 } 77 78 func newClusterConfig(cfg config) (*restclient.Config, error) { 79 rawKubeconfig, err := ioutil.ReadFile(cfg.Gardener.KubeconfigPath) 80 if err != nil { 81 return nil, fmt.Errorf("failed to read Gardener Kubeconfig from path %s: %s", 82 cfg.Gardener.KubeconfigPath, err.Error()) 83 } 84 85 gardenerClusterConfig, err := clientcmd.RESTConfigFromKubeConfig(rawKubeconfig) 86 if err != nil { 87 return nil, fmt.Errorf("failed to create Gardener cluster config: %s", err.Error()) 88 } 89 return gardenerClusterConfig, nil 90 } 91 92 func newKubernetesInterface(kubeconfig *restclient.Config) (kubernetes.Interface, error) { 93 k8sCoreClientSet, err := kubernetes.NewForConfig(kubeconfig) 94 if err != nil { 95 return nil, fmt.Errorf("failed to create kubernetes client: %s", err.Error()) 96 } 97 return k8sCoreClientSet, nil 98 }