Skip to content

Commit 354f300

Browse files
authored
Merge pull request #1348 from KubeKyrie/fix-offline-sprayJob-registry
add SprayJobImageRegistry to kubean-config and update manifest kubesprayImage
2 parents 529e38e + 9df80ab commit 354f300

File tree

12 files changed

+227
-149
lines changed

12 files changed

+227
-149
lines changed

api/cluster/cluster.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cluster
2+
3+
import (
4+
"github.com/kubean-io/kubean-api/constants"
5+
"k8s.io/klog/v2"
6+
"strconv"
7+
)
8+
9+
type ConfigProperty struct {
10+
ClusterOperationsBackEndLimit string `json:"CLUSTER_OPERATIONS_BACKEND_LIMIT"`
11+
SprayJobImageRegistry string `json:"SPRAY_JOB_IMAGE_REGISTRY"`
12+
}
13+
14+
func (config *ConfigProperty) GetClusterOperationsBackEndLimit() int {
15+
value, _ := strconv.Atoi(config.ClusterOperationsBackEndLimit)
16+
if value <= 0 {
17+
klog.Warningf("GetClusterOperationsBackEndLimit and use default value %d", constants.DefaultClusterOperationsBackEndLimit)
18+
return constants.DefaultClusterOperationsBackEndLimit
19+
}
20+
if value >= constants.MaxClusterOperationsBackEndLimit {
21+
klog.Warningf("GetClusterOperationsBackEndLimit and use max value %d", constants.MaxClusterOperationsBackEndLimit)
22+
return constants.MaxClusterOperationsBackEndLimit
23+
}
24+
return value
25+
}

api/constants/constants.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package constants
22

3-
const InfoManifestGlobal = "manifest-global"
3+
const (
4+
InfoManifestGlobal = "manifest-global"
45

5-
const KubeanClusterLabelKey = "clusterName"
6+
KubeanClusterLabelKey = "clusterName"
67

7-
const Hosts_yml = "hosts.yml"
8+
Hosts_yml = "hosts.yml"
89

9-
const Group_vars_yml = "group_vars.yml"
10+
Group_vars_yml = "group_vars.yml"
1011

11-
const SSH_privatekey = "ssh-privatekey"
12+
SSH_privatekey = "ssh-privatekey"
1213

13-
const KubeanClusterHasCompleted = "hasCompleted"
14+
KubeanClusterHasCompleted = "hasCompleted"
1415

15-
const KeySprayRelease = "kubean.io/sprayRelease"
16+
KeySprayRelease = "kubean.io/sprayRelease"
17+
KeySprayCommit = "kubean.io/sprayCommit"
1618

17-
const KeySprayCommit = "kubean.io/sprayCommit"
19+
KubeanConfigMapName = "kubean-config"
20+
DefaultClusterOperationsBackEndLimit = 30
21+
MaxClusterOperationsBackEndLimit = 200
22+
)

charts/kubean/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ metadata:
88
{{- include "kubean.labels" . | nindent 4}}
99
data:
1010
CLUSTER_OPERATIONS_BACKEND_LIMIT: "{{ .Values.kubeanOperator.operationsBackendLimit }}"
11+
SPRAY_JOB_IMAGE_REGISTRY: "{{ .Values.sprayJob.image.registry }}"

pkg/controllers/cluster/controller.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cluster
55

66
import (
77
"context"
8-
"encoding/json"
98
"fmt"
109
"sort"
1110
"strconv"
@@ -27,11 +26,9 @@ import (
2726
)
2827

2928
const (
30-
RequeueAfter = time.Second * 15
31-
KubeanConfigMapName = "kubean-config"
32-
DefaultClusterOperationsBackEndLimit = 30
33-
MaxClusterOperationsBackEndLimit = 200
34-
EliminateScoreAnno = "kubean.io/eliminate-score"
29+
RequeueAfter = time.Second * 15
30+
KubeanConfigMapName = "kubean-config"
31+
EliminateScoreAnno = "kubean.io/eliminate-score"
3532
)
3633

3734
type Controller struct {
@@ -149,40 +146,6 @@ func (c *Controller) CleanExcessClusterOps(cluster *clusterv1alpha1.Cluster, Ops
149146
return true, nil
150147
}
151148

152-
type ConfigProperty struct {
153-
ClusterOperationsBackEndLimit string `json:"CLUSTER_OPERATIONS_BACKEND_LIMIT"`
154-
}
155-
156-
func (config *ConfigProperty) GetClusterOperationsBackEndLimit() int {
157-
value, _ := strconv.Atoi(config.ClusterOperationsBackEndLimit)
158-
if value <= 0 {
159-
klog.Warningf("GetClusterOperationsBackEndLimit and use default value %d", DefaultClusterOperationsBackEndLimit)
160-
return DefaultClusterOperationsBackEndLimit
161-
}
162-
if value >= MaxClusterOperationsBackEndLimit {
163-
klog.Warningf("GetClusterOperationsBackEndLimit and use max value %d", MaxClusterOperationsBackEndLimit)
164-
return MaxClusterOperationsBackEndLimit
165-
}
166-
return value
167-
}
168-
169-
func (c *Controller) FetchKubeanConfigProperty() *ConfigProperty {
170-
configData, err := c.ClientSet.CoreV1().ConfigMaps(util.GetCurrentNSOrDefault()).Get(context.Background(), KubeanConfigMapName, metav1.GetOptions{})
171-
if err != nil {
172-
return &ConfigProperty{}
173-
}
174-
jsonData, err := json.Marshal(configData.Data)
175-
if err != nil {
176-
return &ConfigProperty{}
177-
}
178-
result := &ConfigProperty{}
179-
err = json.Unmarshal(jsonData, result)
180-
if err != nil {
181-
return &ConfigProperty{}
182-
}
183-
return result
184-
}
185-
186149
func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
187150
cluster := &clusterv1alpha1.Cluster{}
188151
if err := c.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
@@ -192,7 +155,7 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
192155
klog.ErrorS(err, "failed to get cluster", "cluster", req.String())
193156
return controllerruntime.Result{RequeueAfter: RequeueAfter}, nil
194157
}
195-
OpsBackupNum := c.FetchKubeanConfigProperty().GetClusterOperationsBackEndLimit()
158+
OpsBackupNum := util.FetchKubeanConfigProperty(c.ClientSet).GetClusterOperationsBackEndLimit()
196159
needRequeue, err := c.CleanExcessClusterOps(cluster, OpsBackupNum)
197160
if err != nil {
198161
klog.ErrorS(err, "failed to clean excess cluster ops", "cluster", cluster.Name)

pkg/controllers/cluster/controller_test.go

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ import (
77
"context"
88
"fmt"
99
"net/http"
10-
"os"
1110
"reflect"
1211
"testing"
1312
"time"
1413

15-
localartifactsetv1alpha1 "github.com/kubean-io/kubean-api/apis/localartifactset/v1alpha1"
16-
manifestv1alpha1 "github.com/kubean-io/kubean-api/apis/manifest/v1alpha1"
17-
"github.com/kubean-io/kubean/pkg/util"
18-
19-
corev1 "k8s.io/api/core/v1"
2014
"k8s.io/apimachinery/pkg/api/meta"
2115
"k8s.io/apimachinery/pkg/runtime"
2216
"k8s.io/apimachinery/pkg/types"
@@ -33,6 +27,8 @@ import (
3327
"github.com/kubean-io/kubean-api/apis"
3428
clusterv1alpha1 "github.com/kubean-io/kubean-api/apis/cluster/v1alpha1"
3529
clusteroperationv1alpha1 "github.com/kubean-io/kubean-api/apis/clusteroperation/v1alpha1"
30+
localartifactsetv1alpha1 "github.com/kubean-io/kubean-api/apis/localartifactset/v1alpha1"
31+
manifestv1alpha1 "github.com/kubean-io/kubean-api/apis/manifest/v1alpha1"
3632
"github.com/kubean-io/kubean-api/constants"
3733
clusterv1alpha1fake "github.com/kubean-io/kubean-api/generated/cluster/clientset/versioned/fake"
3834
clusteroperationv1alpha1fake "github.com/kubean-io/kubean-api/generated/clusteroperation/clientset/versioned/fake"
@@ -177,66 +173,6 @@ func TestCompareClusterConditions(t *testing.T) {
177173
}
178174
}
179175

180-
func TestFetchKubeanConfigProperty(t *testing.T) {
181-
genController := func() *Controller {
182-
return &Controller{
183-
Client: newFakeClient(),
184-
ClientSet: clientsetfake.NewSimpleClientset(),
185-
KubeanClusterSet: clusterv1alpha1fake.NewSimpleClientset(),
186-
KubeanClusterOpsSet: clusteroperationv1alpha1fake.NewSimpleClientset(),
187-
}
188-
}
189-
tests := []struct {
190-
name string
191-
controller *Controller
192-
args func(controller *Controller)
193-
want int
194-
}{
195-
{
196-
name: "no config and default value",
197-
controller: genController(),
198-
args: func(controller *Controller) {
199-
// do nothing
200-
},
201-
want: 30,
202-
},
203-
{
204-
name: "the config has been set to 25",
205-
controller: genController(),
206-
args: func(controller *Controller) {
207-
os.Setenv("POD_NAMESPACE", "")
208-
configMap := &corev1.ConfigMap{}
209-
configMap.Name = KubeanConfigMapName
210-
configMap.Namespace = util.GetCurrentNSOrDefault()
211-
configMap.Data = map[string]string{"CLUSTER_OPERATIONS_BACKEND_LIMIT": "25"}
212-
controller.ClientSet.CoreV1().ConfigMaps(util.GetCurrentNSOrDefault()).Create(context.Background(), configMap, metav1.CreateOptions{})
213-
},
214-
want: 25,
215-
},
216-
{
217-
name: "the config has been set to 10000",
218-
controller: genController(),
219-
args: func(controller *Controller) {
220-
os.Setenv("POD_NAMESPACE", "")
221-
configMap := &corev1.ConfigMap{}
222-
configMap.Name = KubeanConfigMapName
223-
configMap.Namespace = util.GetCurrentNSOrDefault()
224-
configMap.Data = map[string]string{"CLUSTER_OPERATIONS_BACKEND_LIMIT": "10000"}
225-
controller.ClientSet.CoreV1().ConfigMaps(util.GetCurrentNSOrDefault()).Create(context.Background(), configMap, metav1.CreateOptions{})
226-
},
227-
want: 200,
228-
},
229-
}
230-
for _, test := range tests {
231-
t.Run(test.name, func(t *testing.T) {
232-
test.args(test.controller)
233-
if test.controller.FetchKubeanConfigProperty().GetClusterOperationsBackEndLimit() != test.want {
234-
t.Fatal()
235-
}
236-
})
237-
}
238-
}
239-
240176
func TestSortClusterOperationsByCreation(t *testing.T) {
241177
controller := &Controller{
242178
Client: newFakeClient(),

pkg/controllers/infomanifest/controller.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,8 @@ func (c *Controller) UpdateLocalService(manifests []manifestv1alpha1.Manifest) b
190190

191191
// UpdateLocalAvailableImage update image infos into status.
192192
func (c *Controller) UpdateLocalAvailableImage(manifests []manifestv1alpha1.Manifest) {
193+
imageRepo := util.FetchKubeanConfigProperty(c.ClientSet).SprayJobImageRegistry
193194
for _, manifest := range manifests {
194-
imageRepo := "ghcr.m.daocloud.io"
195-
if len(manifest.Spec.LocalService.GetGHCRImageRepo()) != 0 {
196-
imageRepo = manifest.Spec.LocalService.GetGHCRImageRepo() // ghcr.io
197-
}
198195
var newImageName string
199196
sprayRelease := manifest.ObjectMeta.Annotations[constants.KeySprayRelease]
200197
sprayCommit := manifest.ObjectMeta.Annotations[constants.KeySprayCommit]

pkg/controllers/infomanifest/controller_test.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"net/http"
10+
"os"
1011
"reflect"
1112
"testing"
1213
"time"
@@ -37,6 +38,7 @@ import (
3738
"github.com/kubean-io/kubean-api/constants"
3839
localartifactsetv1alpha1fake "github.com/kubean-io/kubean-api/generated/localartifactset/clientset/versioned/fake"
3940
manifestv1alpha1fake "github.com/kubean-io/kubean-api/generated/manifest/clientset/versioned/fake"
41+
"github.com/kubean-io/kubean/pkg/util"
4042
)
4143

4244
func newFakeClient() client.Client {
@@ -252,33 +254,27 @@ func Test_UpdateLocalAvailableImage(t *testing.T) {
252254
want string
253255
}{
254256
{
255-
name: "update local kubespray image with ghcr.io",
257+
name: "update local kubespray image with ghcr.m.daocloud.io",
256258
arg: func() {
257259
manifest.Spec = manifestv1alpha1.Spec{
258260
KubeanVersion: "123",
259261
}
260262
manifest, _ := controller.InfoManifestClientSet.KubeanV1alpha1().Manifests().Update(context.Background(), manifest, metav1.UpdateOptions{})
261-
controller.UpdateLocalAvailableImage([]manifestv1alpha1.Manifest{*manifest})
262-
},
263-
want: "ghcr.m.daocloud.io/kubean-io/spray-job:123",
264-
},
265-
{
266-
name: "update local kubespray image with local registry",
267-
arg: func() {
268-
manifest.Spec = manifestv1alpha1.Spec{
269-
LocalService: manifestv1alpha1.LocalService{ImageRepo: map[manifestv1alpha1.ImageRepoType]string{
270-
"dockerImageRepo": "abc.io",
271-
"githubImageRepo": "ghcr.io",
272-
}},
273-
KubeanVersion: "123456",
274-
}
275-
manifest, err := controller.InfoManifestClientSet.KubeanV1alpha1().Manifests().Update(context.Background(), manifest, metav1.UpdateOptions{})
276-
if err != nil {
277-
t.Error(err)
263+
os.Setenv("POD_NAMESPACE", "")
264+
configMap := &corev1.ConfigMap{
265+
ObjectMeta: metav1.ObjectMeta{
266+
Namespace: util.GetCurrentNSOrDefault(),
267+
Name: "kubean-config",
268+
},
269+
Data: map[string]string{
270+
"CLUSTER_OPERATIONS_BACKEND_LIMIT": "10000",
271+
"SPRAY_JOB_IMAGE_REGISTRY": "ghcr.m.daocloud.io",
272+
},
278273
}
274+
controller.ClientSet.CoreV1().ConfigMaps(util.GetCurrentNSOrDefault()).Create(context.Background(), configMap, metav1.CreateOptions{})
279275
controller.UpdateLocalAvailableImage([]manifestv1alpha1.Manifest{*manifest})
280276
},
281-
want: "ghcr.io/kubean-io/spray-job:123456",
277+
want: "ghcr.m.daocloud.io/kubean-io/spray-job:123",
282278
},
283279
}
284280

pkg/util/cluster.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ package util
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"os"
1011
"strings"
1112

1213
apierrors "k8s.io/apimachinery/pkg/api/errors"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/apimachinery/pkg/runtime"
1416
"k8s.io/client-go/kubernetes"
17+
"k8s.io/client-go/kubernetes/scheme"
1518

1619
"github.com/kubean-io/kubean-api/apis"
1720
clusterv1alpha1 "github.com/kubean-io/kubean-api/apis/cluster/v1alpha1"
1821
clusteroperationv1alpha1 "github.com/kubean-io/kubean-api/apis/clusteroperation/v1alpha1"
1922
localartifactsetv1alpha1 "github.com/kubean-io/kubean-api/apis/localartifactset/v1alpha1"
2023
manifestv1alpha1 "github.com/kubean-io/kubean-api/apis/manifest/v1alpha1"
21-
22-
"k8s.io/apimachinery/pkg/runtime"
23-
"k8s.io/client-go/kubernetes/scheme"
24+
"github.com/kubean-io/kubean-api/cluster"
25+
"github.com/kubean-io/kubean-api/constants"
2426
)
2527

2628
// aggregatedScheme aggregates Kubernetes and extended schemes.
@@ -119,3 +121,20 @@ func GetCurrentRunningPodName() string {
119121
}
120122
return "default"
121123
}
124+
125+
func FetchKubeanConfigProperty(client kubernetes.Interface) *cluster.ConfigProperty {
126+
configData, err := client.CoreV1().ConfigMaps(GetCurrentNSOrDefault()).Get(context.Background(), constants.KubeanConfigMapName, metav1.GetOptions{})
127+
if err != nil {
128+
return &cluster.ConfigProperty{}
129+
}
130+
jsonData, err := json.Marshal(configData.Data)
131+
if err != nil {
132+
return &cluster.ConfigProperty{}
133+
}
134+
result := &cluster.ConfigProperty{}
135+
err = json.Unmarshal(jsonData, result)
136+
if err != nil {
137+
return &cluster.ConfigProperty{}
138+
}
139+
return result
140+
}

0 commit comments

Comments
 (0)