Skip to content

Commit

Permalink
Fix uninstallation
Browse files Browse the repository at this point in the history
  • Loading branch information
barchw committed Dec 13, 2024
1 parent 139273b commit 71a3b88
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ COPY cmd/ cmd/
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -tags ${GO_BUILD_TAGS} -a -ldflags="-X github.com/kyma-project/istio/operator/internal/resources.version=${VERSION}" -o manager main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -a -o istio_install cmd/istio-install/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -tags ${GO_BUILD_TAGS} -ldflags="-s -w -X github.com/kyma-project/istio/operator/internal/resources.version=${VERSION}" -o manager main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -ldflags="-s -w" -o istio_install cmd/istio-install/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
134 changes: 69 additions & 65 deletions internal/reconciliations/istio/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ package istio

import (
"context"
"fmt"
"istio.io/istio/istioctl/pkg/install/k8sversion"
"istio.io/istio/operator/pkg/uninstall"
"istio.io/istio/operator/pkg/util/progress"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/kube"
corev1 "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"os"
"os/exec"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -80,71 +92,63 @@ func (c *IstioClient) Install(mergedIstioOperatorPath string) error {
}

func (c *IstioClient) Uninstall(ctx context.Context) error {
// TODO: Implement uninstallation
//// We don't use any revision capabilities yet
//defaultRevision := ""
//
//rc, err := kube.DefaultRestConfig("", "", func(config *rest.Config) {
// config.QPS = 50
// config.Burst = 100
//})
//if err != nil {
// return fmt.Errorf("failed to create default REST config: %v", err)
//}
//
//kubeClient, err := kube.NewClient(kube.NewClientConfigForRestConfig(rc), "")
//if err != nil {
// return fmt.Errorf("failed to create Istio kube client: %v", err)
//}
//
//if err := k8sversion.IsK8VersionSupported(kubeClient, c.consoleLogger); err != nil {
// return fmt.Errorf("check failed for minimum supported Kubernetes version: %v", err)
//}
//
//ctrlClient, err := client.New(kubeClient.RESTConfig(), client.Options{Scheme: kube.IstioScheme})
//if err != nil {
// return fmt.Errorf("failed to create Kubernetes ctrl client: %v", err)
//}
//
//emptyiops := &iopv1alpha1.IstioOperatorSpec{Profile: "empty", Revision: defaultRevision}
//iop, err := translate.IOPStoIOP(emptyiops, "empty", iopv1alpha1.Namespace(emptyiops))
//if err != nil {
// return err
//}
//
//opts := &helmreconciler.Options{DryRun: false, Log: c.consoleLogger, ProgressLog: progress.NewLog()}
//h, err := helmreconciler.NewHelmReconciler(ctrlClient, kubeClient, iop, opts)
//if err != nil {
// return fmt.Errorf("failed to create reconciler: %v", err)
//}
//
//objectsList, err := h.GetPrunedResources(defaultRevision, true, "")
//if err != nil {
// return err
//}
//
//ctrl.Log.Info(istio.AllResourcesRemovedWarning)
//
//if err := h.DeleteObjectsList(objectsList, ""); err != nil {
// return fmt.Errorf("failed to delete control plane resources by revision: %v", err)
//}
//ctrl.Log.Info("Deletion of istio resources completed")
//
//deletePolicy := metav1.DeletePropagationForeground
//// We need to manually delete the control plane namespace from Istio because the namespace is not removed by default.
//err = ctrlClient.Delete(ctx, &v1.Namespace{
// ObjectMeta: metav1.ObjectMeta{
// Name: constants.IstioSystemNamespace,
// },
//}, &ctrlclient.DeleteOptions{
// PropagationPolicy: &deletePolicy,
//})
//if err != nil && !apiErrors.IsNotFound(err) {
// return err
//}
//ctrl.Log.Info("Deleted istio control plane namespace", "namespace", constants.IstioSystemNamespace)
//
//opts.ProgressLog.SetState(progress.StateUninstallComplete)
rc, err := kube.DefaultRestConfig("", "", func(config *rest.Config) {
config.QPS = 50
config.Burst = 100
})
if err != nil {
return fmt.Errorf("failed to create default REST config: %v", err)
}

kubeClient, err := kube.NewCLIClient(kube.NewClientConfigForRestConfig(rc))
if err != nil {
return fmt.Errorf("failed to create Istio kube client: %v", err)
}

if err := k8sversion.IsK8VersionSupported(kubeClient, c.consoleLogger); err != nil {
return fmt.Errorf("check failed for minimum supported Kubernetes version: %v", err)
}

ctrlClient, err := client.New(kubeClient.RESTConfig(), client.Options{Scheme: kube.IstioScheme})
if err != nil {
return fmt.Errorf("failed to create Kubernetes ctrl client: %v", err)
}

pl := progress.NewLog()

objectsList, err := uninstall.GetPrunedResources(kubeClient, "", "", "default", false)
if err != nil {
return err
}

ctrl.Log.Info(istio.AllResourcesRemovedWarning)

consoleLogger := CreateIstioLibraryLogger()
if err := ConfigureIstioLogScopes(); err != nil {
consoleLogger.LogAndError("Failed to configure Istio log: ", err)
return err
}

if err := uninstall.DeleteObjectsList(kubeClient, false, consoleLogger, objectsList); err != nil {
return fmt.Errorf("failed to delete control plane resources by revision: %v", err)
}
ctrl.Log.Info("Deletion of istio resources completed")

deletePolicy := metav1.DeletePropagationForeground
// We need to manually delete the control plane namespace from Istio because the namespace is not removed by default.
err = ctrlClient.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: constants.IstioSystemNamespace,
},
}, &client.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil && !apiErrors.IsNotFound(err) {
return err
}
ctrl.Log.Info("Deleted istio control plane namespace", "namespace", constants.IstioSystemNamespace)

pl.SetState(progress.StateUninstallComplete)

return nil
}
Expand Down

0 comments on commit 71a3b88

Please sign in to comment.