-
Notifications
You must be signed in to change notification settings - Fork 479
/
kube_state_metrics.go
158 lines (131 loc) · 5.55 KB
/
kube_state_metrics.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
149
150
151
152
153
154
155
156
157
158
// Copyright 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 kubestatemetrics
import (
"context"
"fmt"
"time"
"sigs.k8s.io/controller-runtime/pkg/client"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/component"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
"github.com/gardener/gardener/pkg/utils/managedresources"
secretsmanager "github.com/gardener/gardener/pkg/utils/secrets/manager"
)
const (
// ManagedResourceName is the name of the managed resource for seeds.
ManagedResourceName = "kube-state-metrics"
shootManagedResourceName = "shoot-core-" + ManagedResourceName
containerName = "kube-state-metrics"
labelKeyComponent = "component"
labelKeyType = "type"
labelValueComponent = "kube-state-metrics"
port = 8080
)
// Interface contains functions for a kube-state-metrics deployer.
type Interface interface {
component.DeployWaiter
component.MonitoringComponent
}
// New creates a new instance of DeployWaiter for the kube-state-metrics.
func New(
client client.Client,
namespace string,
secretsManager secretsmanager.Interface,
values Values,
) Interface {
k := &kubeStateMetrics{
client: client,
secretsManager: secretsManager,
namespace: namespace,
values: values,
}
if values.ClusterType == component.ClusterTypeSeed {
k.registry = managedresources.NewRegistry(kubernetes.SeedScheme, kubernetes.SeedCodec, kubernetes.SeedSerializer)
} else {
k.registry = managedresources.NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer)
}
return k
}
type kubeStateMetrics struct {
client client.Client
secretsManager secretsmanager.Interface
namespace string
values Values
registry *managedresources.Registry
}
// Values is a set of configuration values for the kube-state-metrics.
type Values struct {
// ClusterType specifies the type of the cluster to which kube-state-metrics is being deployed.
// For seeds, all resources are being deployed as part of a ManagedResource.
// For shoots, the kube-state-metrics runs in the shoot namespace in the seed as part of the control plane. Hence,
// only the runtime resources (like Deployment, Service, etc.) are being deployed directly (with the client). All
// other application-related resources (like RBAC roles, CRD, etc.) are deployed as part of a ManagedResource.
ClusterType component.ClusterType
// Image is the container image.
Image string
// PriorityClassName is the name of the priority class.
PriorityClassName string
// Replicas is the number of replicas.
Replicas int32
// IsWorkerless specifies whether the cluster has worker nodes.
IsWorkerless bool
}
func (k *kubeStateMetrics) Deploy(ctx context.Context) error {
var (
genericTokenKubeconfigSecretName string
shootAccessSecret *gardenerutils.AccessSecret
)
if k.values.ClusterType == component.ClusterTypeShoot {
genericTokenKubeconfigSecret, found := k.secretsManager.Get(v1beta1constants.SecretNameGenericTokenKubeconfig)
if !found {
return fmt.Errorf("secret %q not found", v1beta1constants.SecretNameGenericTokenKubeconfig)
}
genericTokenKubeconfigSecretName = genericTokenKubeconfigSecret.Name
shootAccessSecret = k.newShootAccessSecret()
if err := shootAccessSecret.Reconcile(ctx, k.client); err != nil {
return err
}
}
return component.DeployResourceConfigs(ctx, k.client, k.namespace, k.values.ClusterType, k.managedResourceName(), k.registry, k.getResourceConfigs(genericTokenKubeconfigSecretName, shootAccessSecret))
}
func (k *kubeStateMetrics) Destroy(ctx context.Context) error {
if err := component.DestroyResourceConfigs(ctx, k.client, k.namespace, k.values.ClusterType, k.managedResourceName(), k.getResourceConfigs("", nil)); err != nil {
return err
}
if k.values.ClusterType == component.ClusterTypeShoot {
return client.IgnoreNotFound(k.client.Delete(ctx, k.newShootAccessSecret().Secret))
}
return nil
}
// TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy
// or deleted.
var TimeoutWaitForManagedResource = 2 * time.Minute
func (k *kubeStateMetrics) Wait(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
return managedresources.WaitUntilHealthy(timeoutCtx, k.client, k.namespace, k.managedResourceName())
}
func (k *kubeStateMetrics) WaitCleanup(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
return managedresources.WaitUntilDeleted(timeoutCtx, k.client, k.namespace, k.managedResourceName())
}
func (k *kubeStateMetrics) managedResourceName() string {
if k.values.ClusterType == component.ClusterTypeSeed {
return ManagedResourceName
}
return shootManagedResourceName
}