Skip to content

Commit

Permalink
major struggle
Browse files Browse the repository at this point in the history
Fixes: #1794

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Jul 13, 2023
1 parent 62eb54a commit 0b8ef19
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 91 deletions.
25 changes: 10 additions & 15 deletions clustermesh/clustermesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ func (k *K8sClusterMesh) patchConfig(ctx context.Context, client k8sClusterMeshI

// getClientsForConnect returns a k8s.Client for the local and remote cluster, respectively
func (k *K8sClusterMesh) getClientsForConnect() (*k8s.Client, *k8s.Client, error) {
remoteClient, err := k8s.NewClient(k.params.DestinationContext, "")
remoteClient, err := k8s.NewClient(k.params.DestinationContext, "", k.params.Namespace)
if err != nil {
return nil, nil, fmt.Errorf(
"unable to create Kubernetes client to access remote cluster %q: %w",
Expand Down Expand Up @@ -1113,7 +1113,7 @@ func (k *K8sClusterMesh) disconnectCluster(ctx context.Context, src, dst k8sClus
}

func (k *K8sClusterMesh) Disconnect(ctx context.Context) error {
remoteCluster, err := k8s.NewClient(k.params.DestinationContext, "")
remoteCluster, err := k8s.NewClient(k.params.DestinationContext, "", k.params.Namespace)
if err != nil {
return fmt.Errorf("unable to create Kubernetes client to access remote cluster %q: %w", k.params.DestinationContext, err)
}
Expand Down Expand Up @@ -1945,7 +1945,7 @@ func EnableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Parameter
ResetValues: false,
ReuseValues: true,
}
_, err = helm.Upgrade(ctx, k8sClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, k8sClient.HelmActionConfig, upgradeParams)
return err
}

Expand All @@ -1965,17 +1965,12 @@ func DisableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Paramete
ResetValues: false,
ReuseValues: true,
}
_, err = helm.Upgrade(ctx, k8sClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, k8sClient.HelmActionConfig, upgradeParams)
return err
}

func getRelease(kc *k8s.Client, namespace string) (*release.Release, error) {
client := kc.RESTClientGetter
release, err := helm.GetCurrentRelease(client, namespace, defaults.HelmReleaseName)
if err != nil {
return nil, err
}
return release, nil
func getRelease(kc *k8s.Client, name string) (*release.Release, error) {
return kc.HelmActionConfig.Releases.Last(name)
}

// validateCAMatch determines if the certificate authority certificate being
Expand Down Expand Up @@ -2084,7 +2079,7 @@ func (k *K8sClusterMesh) ConnectWithHelm(ctx context.Context) error {
// Enable clustermesh using a Helm Upgrade command against our target cluster
k.Log("ℹ️ Configuring Cilium in cluster '%s' to connect to cluster '%s'",
localClient.ClusterName(), remoteClient.ClusterName())
_, err = helm.Upgrade(ctx, localClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, localClient.HelmActionConfig, upgradeParams)
if err != nil {
return err
}
Expand All @@ -2093,7 +2088,7 @@ func (k *K8sClusterMesh) ConnectWithHelm(ctx context.Context) error {
k.Log("ℹ️ Configuring Cilium in cluster '%s' to connect to cluster '%s'",
remoteClient.ClusterName(), localClient.ClusterName())
upgradeParams.Values = remoteHelmValues
_, err = helm.Upgrade(ctx, remoteClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, remoteClient.HelmActionConfig, upgradeParams)
if err != nil {
return err
}
Expand Down Expand Up @@ -2187,15 +2182,15 @@ func (k *K8sClusterMesh) DisconnectWithHelm(ctx context.Context) error {
// Disconnect clustermesh using a Helm Upgrade command against our target cluster
k.Log("ℹ️ Configuring Cilium in cluster '%s' to disconnect from cluster '%s'",
localClient.ClusterName(), remoteClient.ClusterName())
if _, err = helm.Upgrade(ctx, localClient.RESTClientGetter, upgradeParams); err != nil {
if _, err = helm.Upgrade(ctx, localClient.HelmActionConfig, upgradeParams); err != nil {
return err
}

// Disconnect clustermesh using a Helm Upgrade command against the remote cluster
k.Log("ℹ️ Configuring Cilium in cluster '%s' to disconnect from cluster '%s'",
remoteClient.ClusterName(), localClient.ClusterName())
upgradeParams.Values = remoteHelmValues
if _, err = helm.Upgrade(ctx, remoteClient.RESTClientGetter, upgradeParams); err != nil {
if _, err = helm.Upgrade(ctx, remoteClient.HelmActionConfig, upgradeParams); err != nil {
return err
}
k.Log("✅ Disconnected clusters %s and %s!", localClient.ClusterName(), remoteClient.ClusterName())
Expand Down
2 changes: 1 addition & 1 deletion connectivity/check/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func (ct *ConnectivityTest) initClients(ctx context.Context) error {
ct.params.SingleNode = true
}
} else if ct.params.MultiCluster != "" {
dst, err := k8s.NewClient(ct.params.MultiCluster, "")
dst, err := k8s.NewClient(ct.params.MultiCluster, "", ct.params.CiliumNamespace)
if err != nil {
return fmt.Errorf("unable to create Kubernetes client for remote cluster %q: %w", ct.params.MultiCluster, err)
}
Expand Down
4 changes: 2 additions & 2 deletions hubble/hubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ func EnableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Parameter
ResetValues: false,
ReuseValues: true,
}
_, err = helm.Upgrade(ctx, k8sClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, k8sClient.HelmActionConfig, upgradeParams)
return err
}

Expand All @@ -882,6 +882,6 @@ func DisableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Paramete
ResetValues: false,
ReuseValues: true,
}
_, err = helm.Upgrade(ctx, k8sClient.RESTClientGetter, upgradeParams)
_, err = helm.Upgrade(ctx, k8sClient.HelmActionConfig, upgradeParams)
return err
}
2 changes: 1 addition & 1 deletion install/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (k *K8sUninstaller) uninstallCerts(ctx context.Context) (err error) {

func (k *K8sInstaller) installCerts(ctx context.Context) error {
if k.params.InheritCA != "" {
caCluster, err := k8s.NewClient(k.params.InheritCA, "")
caCluster, err := k8s.NewClient(k.params.InheritCA, "", k.params.Namespace)
if err != nil {
return fmt.Errorf("unable to create Kubernetes client to derive CA from: %w", err)
}
Expand Down
13 changes: 2 additions & 11 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/yaml"

"github.com/cilium/cilium/api/v1/models"
Expand Down Expand Up @@ -1018,7 +1017,7 @@ func (k *K8sInstaller) RollbackInstallation(ctx context.Context) {
}
}

func (k *K8sInstaller) InstallWithHelm(ctx context.Context, k8sClient genericclioptions.RESTClientGetter) error {
func (k *K8sInstaller) InstallWithHelm(ctx context.Context, k8sClient *k8s.Client) error {
if k.params.ListVersions {
return k.listVersions()
}
Expand All @@ -1029,15 +1028,7 @@ func (k *K8sInstaller) InstallWithHelm(ctx context.Context, k8sClient genericcli
if err != nil {
return err
}
actionConfig := action.Configuration{}
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
// TODO(michi) Make the logger configurable
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, k.params.Namespace, helmDriver, logger); err != nil {
return err
}
helmClient := action.NewInstall(&actionConfig)
helmClient := action.NewInstall(k8sClient.HelmActionConfig)
helmClient.ReleaseName = defaults.HelmReleaseName
helmClient.Namespace = k.params.Namespace
helmClient.Wait = k.params.Wait
Expand Down
13 changes: 2 additions & 11 deletions install/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"helm.sh/helm/v3/pkg/action"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/cilium/cilium-cli/clustermesh"
"github.com/cilium/cilium-cli/defaults"
Expand Down Expand Up @@ -70,16 +69,8 @@ func (k *K8sUninstaller) Log(format string, a ...interface{}) {
fmt.Fprintf(k.params.Writer, format+"\n", a...)
}

func (k *K8sUninstaller) UninstallWithHelm(ctx context.Context, k8sClient genericclioptions.RESTClientGetter) error {
actionConfig := action.Configuration{}
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
// TODO(michi) Make the logger configurable
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, k.params.Namespace, helmDriver, logger); err != nil {
return err
}
helmClient := action.NewUninstall(&actionConfig)
func (k *K8sUninstaller) UninstallWithHelm(ctx context.Context, actionConfig *action.Configuration) error {
helmClient := action.NewUninstall(actionConfig)
helmClient.Wait = k.params.Wait
helmClient.Timeout = k.params.Timeout
if _, err := helmClient.Run(defaults.HelmReleaseName); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions install/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/yaml"

"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/internal/helm"
"github.com/cilium/cilium-cli/internal/utils"
"github.com/cilium/cilium-cli/k8s"
"github.com/cilium/cilium-cli/status"
)

Expand Down Expand Up @@ -162,7 +162,7 @@ func upgradeDeployment(ctx context.Context, k *K8sInstaller, params upgradeDeplo
return nil
}

func (k *K8sInstaller) UpgradeWithHelm(ctx context.Context, k8sClient genericclioptions.RESTClientGetter) error {
func (k *K8sInstaller) UpgradeWithHelm(ctx context.Context, k8sClient *k8s.Client) error {
if k.params.ListVersions {
return k.listVersions()
}
Expand All @@ -189,7 +189,7 @@ func (k *K8sInstaller) UpgradeWithHelm(ctx context.Context, k8sClient genericcli
DryRun: k.params.DryRun,
DryRunHelmValues: k.params.DryRunHelmValues,
}
release, err := helm.Upgrade(ctx, k8sClient, upgradeParams)
release, err := helm.Upgrade(ctx, k8sClient.HelmActionConfig, upgradeParams)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func NewCiliumCommand(hooks Hooks) *cobra.Command {
case "completion", "help":
return nil
}
fmt.Println("michi", namespace)

c, err := k8s.NewClient(contextName, "")
c, err := k8s.NewClient(contextName, "", namespace)
if err != nil {
return fmt.Errorf("unable to create Kubernetes client: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ cilium install --context kind-cluster1 --helm-set cluster.id=1 --helm-set cluste
return err
}
cmd.SilenceUsage = true
if err := installer.InstallWithHelm(context.Background(), k8sClient.RESTClientGetter); err != nil {
if err := installer.InstallWithHelm(context.Background(), k8sClient); err != nil {
fatalf("Unable to install Cilium: %s", err)
}
return nil
Expand Down Expand Up @@ -319,7 +319,7 @@ func newCmdUninstallWithHelm() *cobra.Command {
cc.UninstallResources(ctx, params.Wait)
}
uninstaller := install.NewK8sUninstaller(k8sClient, params)
if err := uninstaller.UninstallWithHelm(ctx, k8sClient.RESTClientGetter); err != nil {
if err := uninstaller.UninstallWithHelm(ctx, k8sClient.HelmActionConfig); err != nil {
fatalf("Unable to uninstall Cilium: %s", err)
}
return nil
Expand Down Expand Up @@ -359,7 +359,7 @@ cilium upgrade --helm-set cluster.id=1 --helm-set cluster.name=cluster1
return err
}
cmd.SilenceUsage = true
if err := installer.UpgradeWithHelm(context.Background(), k8sClient.RESTClientGetter); err != nil {
if err := installer.UpgradeWithHelm(context.Background(), k8sClient); err != nil {
fatalf("Unable to upgrade Cilium: %s", err)
}
return nil
Expand Down
44 changes: 4 additions & 40 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"helm.sh/helm/v3/pkg/releaseutil"
"helm.sh/helm/v3/pkg/strvals"
corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var settings = cli.New()
Expand Down Expand Up @@ -478,25 +477,6 @@ func resolveChartVersion(versionFlag string, repository string) (semver2.Version
return version, helmChart, nil
}

// GetCurrentRelease gets the currently deployed release
func GetCurrentRelease(
k8sClient genericclioptions.RESTClientGetter,
namespace, name string,
) (*release.Release, error) {
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
actionConfig := action.Configuration{}
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, namespace, helmDriver, logger); err != nil {
return nil, err
}
currentRelease, err := actionConfig.Releases.Last(name)
if err != nil {
return nil, err
}
return currentRelease, nil
}

// UpgradeParameters contains parameters for helm upgrade operation.
type UpgradeParameters struct {
// Namespace in which the Helm release is installed.
Expand Down Expand Up @@ -526,18 +506,9 @@ type UpgradeParameters struct {
// Upgrade upgrades the existing Helm release with the given Helm chart and values
func Upgrade(
ctx context.Context,
k8sClient genericclioptions.RESTClientGetter,
actionConfig *action.Configuration,
params UpgradeParameters,
) (*release.Release, error) {
actionConfig := action.Configuration{}
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
// TODO(michi) Make the logger configurable (search for all instances)
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, params.Namespace, helmDriver, logger); err != nil {
return nil, err
}

if params.Chart == nil {
currentRelease, err := actionConfig.Releases.Last(params.Name)
if err != nil {
Expand All @@ -546,7 +517,7 @@ func Upgrade(
params.Chart = currentRelease.Chart
}

helmClient := action.NewUpgrade(&actionConfig)
helmClient := action.NewUpgrade(actionConfig)
helmClient.Namespace = params.Namespace
helmClient.ResetValues = params.ResetValues
helmClient.ReuseValues = params.ReuseValues
Expand All @@ -567,16 +538,9 @@ type GetParameters struct {

// Get returns the Helm release specified by GetParameters.
func Get(
k8sClient genericclioptions.RESTClientGetter,
actionConfig *action.Configuration,
params GetParameters,
) (*release.Release, error) {
actionConfig := action.Configuration{}
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, params.Namespace, helmDriver, logger); err != nil {
return nil, err
}
helmClient := action.NewGet(&actionConfig)
helmClient := action.NewGet(actionConfig)
return helmClient.Run(params.Name)
}
17 changes: 15 additions & 2 deletions k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"time"

"helm.sh/helm/v3/pkg/action"

"github.com/blang/semver/v4"
"github.com/distribution/distribution/reference"
"helm.sh/helm/v3/pkg/chartutil"
Expand Down Expand Up @@ -65,9 +67,10 @@ type Client struct {
RawConfig clientcmdapi.Config
RESTClientGetter genericclioptions.RESTClientGetter
contextName string
HelmActionConfig *action.Configuration
}

func NewClient(contextName, kubeconfig string) (*Client, error) {
func NewClient(contextName, kubeconfig, ciliumNamespace string) (*Client, error) {
// Register the Cilium types in the default scheme.
_ = ciliumv2.AddToScheme(scheme.Scheme)
_ = ciliumv2alpha1.AddToScheme(scheme.Scheme)
Expand Down Expand Up @@ -118,6 +121,15 @@ func NewClient(contextName, kubeconfig string) (*Client, error) {
contextName = rawConfig.CurrentContext
}

// Initialize Helm action configuration.
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
actionConfig := action.Configuration{}
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(&restClientGetter, ciliumNamespace, helmDriver, logger); err != nil {
return nil, err
}

return &Client{
CiliumClientset: ciliumClientset,
TetragonClientset: tetragonClientset,
Expand All @@ -128,6 +140,7 @@ func NewClient(contextName, kubeconfig string) (*Client, error) {
RawConfig: rawConfig,
RESTClientGetter: &restClientGetter,
contextName: contextName,
HelmActionConfig: &actionConfig,
}, nil
}

Expand Down Expand Up @@ -949,7 +962,7 @@ func (c *Client) GetCiliumVersion(ctx context.Context, p *corev1.Pod) (*semver.V

func (c *Client) GetRunningCiliumVersion(ctx context.Context, namespace string) (string, error) {
if utils.IsInHelmMode() {
release, err := helm.Get(c.RESTClientGetter, helm.GetParameters{
release, err := helm.Get(c.HelmActionConfig, helm.GetParameters{
Namespace: namespace,
Name: defaults.HelmReleaseName,
})
Expand Down
2 changes: 1 addition & 1 deletion status/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func (k *K8sStatusCollector) status(ctx context.Context) *Status {
if !ok {
return fmt.Errorf("failed to initialize Helm client")
}
release, err := helm.Get(client.RESTClientGetter, helm.GetParameters{
release, err := helm.Get(client.HelmActionConfig, helm.GetParameters{
Namespace: k.params.Namespace,
Name: defaults.HelmReleaseName,
})
Expand Down

0 comments on commit 0b8ef19

Please sign in to comment.