This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
in_cluster.go
69 lines (60 loc) · 1.96 KB
/
in_cluster.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
package impl
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/flyteorg/flyteadmin/pkg/executioncluster"
"github.com/flyteorg/flyteadmin/pkg/executioncluster/interfaces"
"github.com/flyteorg/flyteadmin/pkg/flytek8s"
"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// DO NOT USE: only for backwards compatibility
const defaultInClusterTargetID = "id"
type InCluster struct {
target executioncluster.ExecutionTarget
asTargets map[string]*executioncluster.ExecutionTarget
}
func (i InCluster) GetTarget(ctx context.Context, spec *executioncluster.ExecutionTargetSpec) (*executioncluster.ExecutionTarget, error) {
if spec != nil && !(spec.TargetID == "" || spec.TargetID == defaultInClusterTargetID) {
return nil, errors.New(fmt.Sprintf("remote target %s is not supported", spec.TargetID))
}
return &i.target, nil
}
func (i InCluster) GetAllTargets() map[string]*executioncluster.ExecutionTarget {
return i.asTargets
}
func (i InCluster) GetValidTargets() map[string]*executioncluster.ExecutionTarget {
return i.asTargets
}
func NewInCluster(initializationErrorCounter prometheus.Counter, kubeConfig, master string) (interfaces.ClusterInterface, error) {
clientConfig, err := flytek8s.GetRestClientConfig(kubeConfig, master, nil)
if err != nil {
return nil, err
}
flyteClient, err := getRestClientFromKubeConfig(initializationErrorCounter, clientConfig)
if err != nil {
return nil, err
}
kubeClient, err := client.New(clientConfig, client.Options{})
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
target := executioncluster.ExecutionTarget{
Client: kubeClient,
FlyteClient: flyteClient,
DynamicClient: dynamicClient,
Config: *clientConfig,
}
return &InCluster{
target: target,
asTargets: map[string]*executioncluster.ExecutionTarget{
target.ID: &target,
},
}, nil
}