-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathtypes.go
148 lines (118 loc) · 3.42 KB
/
types.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hook
import (
"context"
"fmt"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/apecloud/kubeblocks/pkg/client/clientset/versioned"
)
type UpgradeMetaContext struct {
context.Context
CRDPath string
Version string
Namespace string
CRClient
}
type CRClient struct {
*dynamic.DynamicClient
KBClient *versioned.Clientset
K8sClient *kubernetes.Clientset
CRDClient *clientset.Clientset
}
type UpgradeContext struct {
UpgradeMetaContext
From *Version
To Version
UpdatedObjects map[schema.GroupVersionResource][]client.Object
}
type Version struct {
Major int32
Minor int32
}
func (v Version) String() string {
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
}
// ContextHandler is the interface for a "chunk" of reconciliation. It either
// returns, often by adjusting the current key's place in the queue (i.e. via
// requeue or done) or calls another handler in the chain.
type ContextHandler interface {
IsSkip(*UpgradeContext) (bool, error)
Handle(*UpgradeContext) error
}
type BasedHandler struct {
ContextHandler
}
func (b *BasedHandler) IsSkip(*UpgradeContext) (bool, error) {
return false, nil
}
// func (b *BasedHandler) Handle(*UpgradeContext) error {
// return nil
// }
// ContextHandlerFunc is a function type that implements ContextHandler
type ContextHandlerFunc func(ctx *UpgradeContext) error
func (f ContextHandlerFunc) Handle(ctx *UpgradeContext) error {
return f(ctx)
}
type Workflow []ContextHandlerFunc
func (w Workflow) Do(ctx *UpgradeContext) error {
for _, handler := range w {
if err := handler(ctx); err != nil {
return err
}
}
return nil
}
func (w Workflow) AddStage(stage ContextHandler) Workflow {
return append(w, func(ctx *UpgradeContext) error {
skip, err := stage.IsSkip(ctx)
if err != nil {
return err
}
if !skip {
err = stage.Handle(ctx)
}
return err
})
}
func (w Workflow) WrapStage(stageFn ContextHandlerFunc) Workflow {
return append(w, stageFn)
}
func NewUpgradeWorkflow() Workflow {
return Workflow{}
}
func NewUpgradeContext(ctx context.Context, config *restclient.Config, version string, crd string, ns string) *UpgradeContext {
nVersion, err := NewVersion(version)
CheckErr(err)
return &UpgradeContext{
UpgradeMetaContext: UpgradeMetaContext{
CRClient: CRClient{
DynamicClient: dynamic.NewForConfigOrDie(config),
KBClient: versioned.NewForConfigOrDie(config),
K8sClient: kubernetes.NewForConfigOrDie(config),
CRDClient: clientset.NewForConfigOrDie(config),
},
Context: ctx,
Version: version,
CRDPath: crd,
Namespace: ns,
},
UpdatedObjects: make(map[schema.GroupVersionResource][]client.Object),
To: *nVersion,
}
}