/
identity.go
139 lines (114 loc) · 3.61 KB
/
identity.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
// 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 gardener
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"k8s.io/component-base/version"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/utils"
)
// DetermineIdentity determines the Gardener component identity.
// We want to determine the Docker container id of the currently running instance because we need to identify for still
// ongoing operations whether another instance is still operating the respective Shoots. When running locally, we
// generate a random string because there is no container id.
func DetermineIdentity() (*gardencorev1beta1.Gardener, error) {
var (
validID = regexp.MustCompile(`([0-9a-f]{64})`)
id string
name string
err error
)
name, err = os.Hostname()
if err != nil {
return nil, fmt.Errorf("unable to get hostname: %w", err)
}
// If running inside a Kubernetes cluster (as container) we can read the container id from the proc file system.
// Otherwise generate a random string for the id
if cGroupFile, err := os.Open("/proc/self/cgroup"); err == nil {
defer cGroupFile.Close()
reader := bufio.NewReader(cGroupFile)
var cgroupV1 string
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
// Store cgroup-v1 result for fall back
if strings.HasPrefix(line, "1:name=systemd") {
cgroupV1 = line
}
// Always prefer cgroup-v2
if strings.HasPrefix(line, "0::") {
if containerID := extractID(line); validID.MatchString(containerID) {
id = containerID
break
}
}
}
// Fall-back to cgroup-v1 if possible
if len(id) == 0 && len(cgroupV1) > 0 {
id = extractID(cgroupV1)
}
}
if id == "" {
id, err = utils.GenerateRandomString(64)
if err != nil {
return nil, fmt.Errorf("unable to generate id: %w", err)
}
}
return &gardencorev1beta1.Gardener{
ID: id,
Name: name,
Version: version.Get().GitVersion,
}, nil
}
func extractID(line string) string {
var (
id string
splitBySlash = strings.Split(line, "/")
)
if len(splitBySlash) == 0 {
return ""
}
id = strings.TrimSpace(splitBySlash[len(splitBySlash)-1])
id = strings.TrimSuffix(id, ".scope")
id = strings.TrimPrefix(id, "docker-")
return id
}
// MaintainSeedNameLabels maintains the seed.gardener.cloud/<name>=true labels on the given object.
func MaintainSeedNameLabels(obj client.Object, names ...*string) {
labels := obj.GetLabels()
for k := range labels {
if strings.HasPrefix(k, v1beta1constants.LabelPrefixSeedName) {
delete(labels, k)
}
}
for _, name := range names {
if ptr.Deref(name, "") == "" {
continue
}
if labels == nil {
labels = make(map[string]string)
}
labels[v1beta1constants.LabelPrefixSeedName+*name] = "true"
}
obj.SetLabels(labels)
}