/
resource.go
71 lines (60 loc) · 1.53 KB
/
resource.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
package op
import (
"context"
"github.com/cybozu-go/cke"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
)
type resourceApplyOp struct {
apiserver *cke.Node
resource cke.ResourceDefinition
forceConflicts bool
finished bool
}
// ResourceApplyOp creates or updates a Kubernetes object.
func ResourceApplyOp(apiServer *cke.Node, resource cke.ResourceDefinition, forceConflicts bool) cke.Operator {
return &resourceApplyOp{
apiserver: apiServer,
resource: resource,
forceConflicts: forceConflicts,
}
}
func (o *resourceApplyOp) Name() string {
return "resource-apply"
}
func (o *resourceApplyOp) NextCommand() cke.Commander {
if o.finished {
return nil
}
o.finished = true
return o
}
func (o *resourceApplyOp) Targets() []string {
return []string{
o.apiserver.Address,
}
}
func (o *resourceApplyOp) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
cfg, err := inf.K8sConfig(ctx, o.apiserver)
if err != nil {
return err
}
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return err
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
dyn, err := dynamic.NewForConfig(cfg)
if err != nil {
return err
}
return cke.ApplyResource(ctx, dyn, mapper, inf, o.resource.Definition, o.resource.Revision, true)
}
func (o *resourceApplyOp) Command() cke.Command {
return cke.Command{
Name: "apply-resource",
Target: o.resource.String(),
}
}