Skip to content

Commit b7f36f0

Browse files
committed
dev: separate kube context
1 parent 1fcb411 commit b7f36f0

File tree

12 files changed

+69
-5
lines changed

12 files changed

+69
-5
lines changed

pkg/controller/chi/kube/config-map.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,22 @@ func NewConfigMap(kubeClient kube.Interface) *ConfigMap {
3939
}
4040

4141
func (c *ConfigMap) Create(ctx context.Context, cm *core.ConfigMap) (*core.ConfigMap, error) {
42+
ctx = k8sCtx(ctx)
4243
return c.kubeClient.CoreV1().ConfigMaps(cm.Namespace).Create(ctx, cm, controller.NewCreateOptions())
4344
}
4445

4546
func (c *ConfigMap) Get(ctx context.Context, namespace, name string) (*core.ConfigMap, error) {
47+
ctx = k8sCtx(ctx)
4648
return c.kubeClient.CoreV1().ConfigMaps(namespace).Get(ctx, name, controller.NewGetOptions())
4749
}
4850

4951
func (c *ConfigMap) Update(ctx context.Context, cm *core.ConfigMap) (*core.ConfigMap, error) {
52+
ctx = k8sCtx(ctx)
5053
return c.kubeClient.CoreV1().ConfigMaps(cm.Namespace).Update(ctx, cm, controller.NewUpdateOptions())
5154
}
5255

5356
func (c *ConfigMap) Delete(ctx context.Context, namespace, name string) error {
57+
ctx = k8sCtx(ctx)
5458
c.kubeClient.CoreV1().ConfigMaps(namespace).Delete(ctx, name, controller.NewDeleteOptions())
5559
return poller.New(ctx, fmt.Sprintf("%s/%s", namespace, name)).
5660
WithOptions(poller.NewOptions().FromConfig(chop.Config())).
@@ -63,6 +67,7 @@ func (c *ConfigMap) Delete(ctx context.Context, namespace, name string) error {
6367
}
6468

6569
func (c *ConfigMap) List(ctx context.Context, namespace string, opts meta.ListOptions) ([]core.ConfigMap, error) {
70+
ctx = k8sCtx(ctx)
6671
list, err := c.kubeClient.CoreV1().ConfigMaps(namespace).List(ctx, opts)
6772
if err != nil {
6873
return nil, err

pkg/controller/chi/kube/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kube
16+
17+
import "context"
18+
19+
func k8sCtx(ctx context.Context) context.Context {
20+
return context.Background()
21+
}

pkg/controller/chi/kube/cr.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func NewCR(chopClient chopClientSet.Interface, kubeClient kube.Interface) *CR {
5252
}
5353

5454
func (c *CR) Get(ctx context.Context, namespace, name string) (api.ICustomResource, error) {
55+
ctx = k8sCtx(ctx)
56+
5557
chi, err := c.getCR(ctx, namespace, name)
5658
if err != nil {
5759
return nil, err
@@ -65,10 +67,12 @@ func (c *CR) Get(ctx context.Context, namespace, name string) (api.ICustomResour
6567
}
6668

6769
func (c *CR) getCR(ctx context.Context, namespace, name string) (*api.ClickHouseInstallation, error) {
70+
ctx = k8sCtx(ctx)
6871
return c.chopClient.ClickhouseV1().ClickHouseInstallations(namespace).Get(ctx, name, controller.NewGetOptions())
6972
}
7073

7174
func (c *CR) getCM(ctx context.Context, chi api.ICustomResource) (*core.ConfigMap, error) {
75+
ctx = k8sCtx(ctx)
7276
return NewConfigMap(c.kubeClient).Get(ctx, c.buildCMNamespace(chi), c.buildCMName(chi))
7377
}
7478

@@ -99,7 +103,7 @@ func (c *CR) buildCR(chi *api.ClickHouseInstallation, cm *core.ConfigMap) *api.C
99103
// StatusUpdate updates CR object's Status
100104
func (c *CR) StatusUpdate(ctx context.Context, cr api.ICustomResource, opts commonTypes.UpdateStatusOptions) error {
101105
if util.IsContextDone(ctx) {
102-
log.V(2).Info("task is done")
106+
log.V(1).Info("Reconcile is aborted. cr: %s ", cr.GetName())
103107
return nil
104108
}
105109

@@ -130,7 +134,7 @@ func (c *CR) statusUpdateRetry(ctx context.Context, cr api.ICustomResource, opts
130134
// statusUpdateProcess updates CR object's Status
131135
func (c *CR) statusUpdateProcess(ctx context.Context, icr api.ICustomResource, opts commonTypes.UpdateStatusOptions) error {
132136
if util.IsContextDone(ctx) {
133-
log.V(2).Info("task is done")
137+
log.V(1).Info("Reconcile is aborted. cr: %s ", icr.GetName())
134138
return nil
135139
}
136140

pkg/controller/chi/kube/deployment.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ func NewDeployment(kubeClient kube.Interface) *Deployment {
3232
}
3333

3434
func (c *Deployment) Get(namespace, name string) (*apps.Deployment, error) {
35-
return c.kubeClient.AppsV1().Deployments(namespace).Get(controller.NewContext(), name, controller.NewGetOptions())
35+
ctx := k8sCtx(controller.NewContext())
36+
return c.kubeClient.AppsV1().Deployments(namespace).Get(ctx, name, controller.NewGetOptions())
3637
}
3738

3839
func (c *Deployment) Update(deployment *apps.Deployment) (*apps.Deployment, error) {
39-
return c.kubeClient.AppsV1().Deployments(deployment.Namespace).Update(controller.NewContext(), deployment, controller.NewUpdateOptions())
40+
ctx := k8sCtx(controller.NewContext())
41+
return c.kubeClient.AppsV1().Deployments(deployment.Namespace).Update(ctx, deployment, controller.NewUpdateOptions())
4042
}

pkg/controller/chi/kube/event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ func NewEvent(kubeClient kube.Interface) *Event {
3434
}
3535

3636
func (c *Event) Create(ctx context.Context, event *core.Event) (*core.Event, error) {
37+
ctx = k8sCtx(ctx)
3738
return c.kubeClient.CoreV1().Events(event.Namespace).Create(ctx, event, controller.NewCreateOptions())
3839
}

pkg/controller/chi/kube/pdb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,22 @@ func NewPDB(kubeClient kube.Interface) *PDB {
3939
}
4040

4141
func (c *PDB) Create(ctx context.Context, pdb *policy.PodDisruptionBudget) (*policy.PodDisruptionBudget, error) {
42+
ctx = k8sCtx(ctx)
4243
return c.kubeClient.PolicyV1().PodDisruptionBudgets(pdb.Namespace).Create(ctx, pdb, controller.NewCreateOptions())
4344
}
4445

4546
func (c *PDB) Get(ctx context.Context, namespace, name string) (*policy.PodDisruptionBudget, error) {
47+
ctx = k8sCtx(ctx)
4648
return c.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Get(ctx, name, controller.NewGetOptions())
4749
}
4850

4951
func (c *PDB) Update(ctx context.Context, pdb *policy.PodDisruptionBudget) (*policy.PodDisruptionBudget, error) {
52+
ctx = k8sCtx(ctx)
5053
return c.kubeClient.PolicyV1().PodDisruptionBudgets(pdb.Namespace).Update(ctx, pdb, controller.NewUpdateOptions())
5154
}
5255

5356
func (c *PDB) Delete(ctx context.Context, namespace, name string) error {
57+
ctx = k8sCtx(ctx)
5458
c.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Delete(ctx, name, controller.NewDeleteOptions())
5559
return poller.New(ctx, fmt.Sprintf("%s/%s", namespace, name)).
5660
WithOptions(poller.NewOptions().FromConfig(chop.Config())).
@@ -63,6 +67,7 @@ func (c *PDB) Delete(ctx context.Context, namespace, name string) error {
6367
}
6468

6569
func (c *PDB) List(ctx context.Context, namespace string, opts meta.ListOptions) ([]policy.PodDisruptionBudget, error) {
70+
ctx = k8sCtx(ctx)
6671
list, err := c.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).List(ctx, opts)
6772
if err != nil {
6873
return nil, err

pkg/controller/chi/kube/pod.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func (c *Pod) Get(params ...any) (*core.Pod, error) {
6565
default:
6666
panic(any("incorrect number or params"))
6767
}
68-
return c.kubeClient.CoreV1().Pods(namespace).Get(controller.NewContext(), name, controller.NewGetOptions())
68+
ctx := k8sCtx(controller.NewContext())
69+
return c.kubeClient.CoreV1().Pods(namespace).Get(ctx, name, controller.NewGetOptions())
6970
}
7071

7172
func (c *Pod) GetRestartCounters(params ...any) (map[string]int, error) {
@@ -93,6 +94,7 @@ func (c *Pod) GetAll(obj any) []*core.Pod {
9394
}
9495

9596
func (c *Pod) Update(ctx context.Context, pod *core.Pod) (*core.Pod, error) {
97+
ctx = k8sCtx(ctx)
9698
return c.kubeClient.CoreV1().Pods(pod.GetNamespace()).Update(ctx, pod, controller.NewUpdateOptions())
9799
}
98100

@@ -118,5 +120,6 @@ func (c *Pod) getPod(host *api.Host) (pods []*core.Pod) {
118120
}
119121

120122
func (c *Pod) Delete(ctx context.Context, namespace, name string) error {
123+
ctx = k8sCtx(ctx)
121124
return c.kubeClient.CoreV1().Pods(namespace).Delete(ctx, name, controller.NewDeleteOptions())
122125
}

pkg/controller/chi/kube/pvc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,27 @@ func NewPVC(kubeClient kube.Interface) *PVC {
3838
}
3939

4040
func (c *PVC) Create(ctx context.Context, pvc *core.PersistentVolumeClaim) (*core.PersistentVolumeClaim, error) {
41+
ctx = k8sCtx(ctx)
4142
return c.kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(ctx, pvc, controller.NewCreateOptions())
4243
}
4344

4445
func (c *PVC) Get(ctx context.Context, namespace, name string) (*core.PersistentVolumeClaim, error) {
46+
ctx = k8sCtx(ctx)
4547
return c.kubeClient.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, controller.NewGetOptions())
4648
}
4749

4850
func (c *PVC) Update(ctx context.Context, pvc *core.PersistentVolumeClaim) (*core.PersistentVolumeClaim, error) {
51+
ctx = k8sCtx(ctx)
4952
return c.kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Update(ctx, pvc, controller.NewUpdateOptions())
5053
}
5154

5255
func (c *PVC) Delete(ctx context.Context, namespace, name string) error {
56+
ctx = k8sCtx(ctx)
5357
return c.kubeClient.CoreV1().PersistentVolumeClaims(namespace).Delete(ctx, name, controller.NewDeleteOptions())
5458
}
5559

5660
func (c *PVC) List(ctx context.Context, namespace string, opts meta.ListOptions) ([]core.PersistentVolumeClaim, error) {
61+
ctx = k8sCtx(ctx)
5762
list, err := c.kubeClient.CoreV1().PersistentVolumeClaims(namespace).List(ctx, opts)
5863
if err != nil {
5964
return nil, err
@@ -65,6 +70,7 @@ func (c *PVC) List(ctx context.Context, namespace string, opts meta.ListOptions)
6570
}
6671

6772
func (c *PVC) ListForHost(ctx context.Context, host *api.Host) (*core.PersistentVolumeClaimList, error) {
73+
ctx = k8sCtx(ctx)
6874
return c.kubeClient.
6975
CoreV1().
7076
PersistentVolumeClaims(host.Runtime.Address.Namespace).

pkg/controller/chi/kube/replicaset.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ func NewReplicaSet(kubeClient kube.Interface) *ReplicaSet {
3434
}
3535

3636
func (c *ReplicaSet) Get(ctx context.Context, namespace, name string) (*apps.ReplicaSet, error) {
37+
ctx = k8sCtx(ctx)
3738
return c.kubeClient.AppsV1().ReplicaSets(namespace).Get(ctx, name, controller.NewGetOptions())
3839
}
3940

4041
func (c *ReplicaSet) Update(ctx context.Context, replicaSet *apps.ReplicaSet) (*apps.ReplicaSet, error) {
42+
ctx = k8sCtx(ctx)
4143
return c.kubeClient.AppsV1().ReplicaSets(replicaSet.Namespace).Update(ctx, replicaSet, controller.NewUpdateOptions())
4244
}

pkg/controller/chi/kube/secret.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,22 @@ func (c *Secret) Get(ctx context.Context, params ...any) (*core.Secret, error) {
6464
namespace = typedObj.Runtime.Address.Namespace
6565
}
6666
}
67+
ctx = k8sCtx(ctx)
6768
return c.kubeClient.CoreV1().Secrets(namespace).Get(ctx, name, controller.NewGetOptions())
6869
}
6970

7071
func (c *Secret) Create(ctx context.Context, svc *core.Secret) (*core.Secret, error) {
72+
ctx = k8sCtx(ctx)
7173
return c.kubeClient.CoreV1().Secrets(svc.Namespace).Create(ctx, svc, controller.NewCreateOptions())
7274
}
7375

7476
func (c *Secret) Update(ctx context.Context, svc *core.Secret) (*core.Secret, error) {
77+
ctx = k8sCtx(ctx)
7578
return c.kubeClient.CoreV1().Secrets(svc.Namespace).Update(ctx, svc, controller.NewUpdateOptions())
7679
}
7780

7881
func (c *Secret) Delete(ctx context.Context, namespace, name string) error {
82+
ctx = k8sCtx(ctx)
7983
c.kubeClient.CoreV1().Secrets(namespace).Delete(ctx, name, controller.NewDeleteOptions())
8084
return poller.New(ctx, fmt.Sprintf("%s/%s", namespace, name)).
8185
WithOptions(poller.NewOptions().FromConfig(chop.Config())).
@@ -89,6 +93,7 @@ func (c *Secret) Delete(ctx context.Context, namespace, name string) error {
8993
}
9094

9195
func (c *Secret) List(ctx context.Context, namespace string, opts meta.ListOptions) ([]core.Secret, error) {
96+
ctx = k8sCtx(ctx)
9297
list, err := c.kubeClient.CoreV1().Secrets(namespace).List(ctx, opts)
9398
if err != nil {
9499
return nil, err

0 commit comments

Comments
 (0)