forked from grafana/tanka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes.go
83 lines (66 loc) · 1.82 KB
/
kubernetes.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
82
83
package kubernetes
import (
"fmt"
"github.com/Masterminds/semver"
"github.com/grafana/tanka/pkg/kubernetes/client"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)
// Kubernetes exposes methods to work with the Kubernetes orchestrator
type Kubernetes struct {
Env v1alpha1.Environment
// Client (kubectl)
ctl client.Client
// Diffing
differs map[string]Differ // List of diff strategies
}
// Differ is responsible for comparing the given manifests to the cluster and
// returning differences (if any) in `diff(1)` format.
type Differ func(manifest.List) (*string, error)
// New creates a new Kubernetes with an initialized client
func New(env v1alpha1.Environment) (*Kubernetes, error) {
// setup client
ctl, err := client.New(env.Spec.APIServer)
if err != nil {
return nil, err
}
// setup diffing
if env.Spec.DiffStrategy == "" {
env.Spec.DiffStrategy = "native"
if ctl.Info().ServerVersion.LessThan(semver.MustParse("1.13.0")) {
env.Spec.DiffStrategy = "subset"
}
}
k := Kubernetes{
Env: env,
ctl: ctl,
differs: map[string]Differ{
"native": ctl.DiffServerSide,
"subset": SubsetDiffer(ctl),
},
}
return &k, nil
}
// Close runs final cleanup
func (k *Kubernetes) Close() error {
return k.ctl.Close()
}
// DiffOpts allow to specify additional parameters for diff operations
type DiffOpts struct {
// Use `diffstat(1)` to create a histogram of the changes instead
Summarize bool
// Find orphaned resources and include them in the diff
WithPrune bool
// Set the diff-strategy. If unset, the value set in the spec is used
Strategy string
}
// Info about the client, etc.
func (k *Kubernetes) Info() client.Info {
return k.ctl.Info()
}
func objectspec(m manifest.Manifest) string {
return fmt.Sprintf("%s/%s",
m.Kind(),
m.Metadata().Name(),
)
}