-
Notifications
You must be signed in to change notification settings - Fork 35
/
client.go
81 lines (67 loc) · 2.21 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Edgeless Systems GmbH.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package kube
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/cert-manager/cert-manager/pkg/util/cmapichecker"
"github.com/edgelesssys/marblerun/cli/internal/helm"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const versionLabel = "app.kubernetes.io/version"
// NewClient returns the kubernetes Clientset to interact with the k8s API.
func NewClient() (*kubernetes.Clientset, error) {
kubeConfig, err := getRestConfig()
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, fmt.Errorf("creating client: %w", err)
}
return kubeClient, nil
}
// CoordinatorVersion returns the version of the Coordinator deployment.
func CoordinatorVersion(ctx context.Context, namespace string) (string, error) {
kubeClient, err := NewClient()
if err != nil {
return "", err
}
coordinatorDeployment, err := kubeClient.AppsV1().Deployments(namespace).Get(ctx, helm.CoordinatorDeployment, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("retrieving deployment information: %w", err)
}
version := coordinatorDeployment.Labels[versionLabel]
if len(version) <= 0 {
return "", fmt.Errorf("deployment has no label %s", versionLabel)
}
return version, nil
}
// NewCertManagerChecker checks if cert-manager is installed in the cluster.
func NewCertManagerChecker() (cmapichecker.Interface, error) {
kubeConfig, err := getRestConfig()
if err != nil {
return nil, err
}
return cmapichecker.New(kubeConfig, runtime.NewScheme(), "default")
}
func getRestConfig() (*rest.Config, error) {
path := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if path == "" {
homedir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
path = filepath.Join(homedir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName)
}
return clientcmd.BuildConfigFromFlags("", path)
}