Skip to content

Commit e577ed7

Browse files
committed
Merge branch 'master' into add-watch-label
2 parents 75e533d + 5815384 commit e577ed7

File tree

14 files changed

+142
-26
lines changed

14 files changed

+142
-26
lines changed

bootstrap/kubeadm/controllers/kubeadmconfig_controller.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ import (
5353
"sigs.k8s.io/controller-runtime/pkg/source"
5454
)
5555

56+
const (
57+
// KubeadmConfigControllerName defines the controller used when creating clients
58+
KubeadmConfigControllerName = "kubeadmconfig-controller"
59+
)
60+
5661
// InitLocker is a lock that is used around kubeadm init
5762
type InitLocker interface {
5863
Lock(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) bool
@@ -266,7 +271,7 @@ func (r *KubeadmConfigReconciler) refreshBootstrapToken(ctx context.Context, con
266271
log := ctrl.LoggerFrom(ctx)
267272
token := config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token
268273

269-
remoteClient, err := r.remoteClientGetter(ctx, r.Client, util.ObjectKey(cluster))
274+
remoteClient, err := r.remoteClientGetter(ctx, KubeadmConfigControllerName, r.Client, util.ObjectKey(cluster))
270275
if err != nil {
271276
log.Error(err, "Error creating remote cluster client")
272277
return ctrl.Result{}, err
@@ -284,7 +289,7 @@ func (r *KubeadmConfigReconciler) refreshBootstrapToken(ctx context.Context, con
284289
func (r *KubeadmConfigReconciler) rotateMachinePoolBootstrapToken(ctx context.Context, config *bootstrapv1.KubeadmConfig, cluster *clusterv1.Cluster, scope *Scope) (ctrl.Result, error) {
285290
log := ctrl.LoggerFrom(ctx)
286291
log.V(2).Info("Config is owned by a MachinePool, checking if token should be rotated")
287-
remoteClient, err := r.remoteClientGetter(ctx, r.Client, util.ObjectKey(cluster))
292+
remoteClient, err := r.remoteClientGetter(ctx, KubeadmConfigControllerName, r.Client, util.ObjectKey(cluster))
288293
if err != nil {
289294
return ctrl.Result{}, err
290295
}
@@ -757,7 +762,7 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(ctx context.Context, cluste
757762

758763
// if BootstrapToken already contains a token, respect it; otherwise create a new bootstrap token for the node to join
759764
if config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token == "" {
760-
remoteClient, err := r.remoteClientGetter(ctx, r.Client, util.ObjectKey(cluster))
765+
remoteClient, err := r.remoteClientGetter(ctx, KubeadmConfigControllerName, r.Client, util.ObjectKey(cluster))
761766
if err != nil {
762767
return ctrl.Result{}, err
763768
}

cmd/clusterctl/config/zz_generated.bindata.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/machine_controller.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ import (
5252
"sigs.k8s.io/controller-runtime/pkg/source"
5353
)
5454

55+
const (
56+
// MachineControllerName defines the controller used when creating clients
57+
MachineControllerName = "machine-controller"
58+
)
59+
5560
var (
5661
errNilNodeRef = errors.New("noderef is nil")
5762
errLastControlPlaneNode = errors.New("last control plane member")
@@ -488,7 +493,7 @@ func (r *MachineReconciler) isDeleteNodeAllowed(ctx context.Context, cluster *cl
488493
func (r *MachineReconciler) drainNode(ctx context.Context, cluster *clusterv1.Cluster, nodeName string) error {
489494
log := ctrl.LoggerFrom(ctx, "cluster", cluster.Name, "node", nodeName)
490495

491-
restConfig, err := remote.RESTConfig(ctx, r.Client, util.ObjectKey(cluster))
496+
restConfig, err := remote.RESTConfig(ctx, MachineControllerName, r.Client, util.ObjectKey(cluster))
492497
if err != nil {
493498
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")
494499
return nil

controllers/remote/cluster.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package remote
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
"github.com/pkg/errors"
2324
restclient "k8s.io/client-go/rest"
@@ -26,12 +27,16 @@ import (
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
)
2829

30+
const (
31+
defaultClientTimeout = 10 * time.Second
32+
)
33+
2934
// ClusterClientGetter returns a new remote client.
30-
type ClusterClientGetter func(ctx context.Context, c client.Client, cluster client.ObjectKey) (client.Client, error)
35+
type ClusterClientGetter func(ctx context.Context, sourceName string, c client.Client, cluster client.ObjectKey) (client.Client, error)
3136

3237
// NewClusterClient returns a Client for interacting with a remote Cluster using the given scheme for encoding and decoding objects.
33-
func NewClusterClient(ctx context.Context, c client.Client, cluster client.ObjectKey) (client.Client, error) {
34-
restConfig, err := RESTConfig(ctx, c, cluster)
38+
func NewClusterClient(ctx context.Context, sourceName string, c client.Client, cluster client.ObjectKey) (client.Client, error) {
39+
restConfig, err := RESTConfig(ctx, sourceName, c, cluster)
3540
if err != nil {
3641
return nil, err
3742
}
@@ -43,7 +48,7 @@ func NewClusterClient(ctx context.Context, c client.Client, cluster client.Objec
4348
}
4449

4550
// RESTConfig returns a configuration instance to be used with a Kubernetes client.
46-
func RESTConfig(ctx context.Context, c client.Reader, cluster client.ObjectKey) (*restclient.Config, error) {
51+
func RESTConfig(ctx context.Context, sourceName string, c client.Reader, cluster client.ObjectKey) (*restclient.Config, error) {
4752
kubeConfig, err := kcfg.FromSecret(ctx, c, cluster)
4853
if err != nil {
4954
return nil, errors.Wrapf(err, "failed to retrieve kubeconfig secret for Cluster %s/%s", cluster.Namespace, cluster.Name)
@@ -54,5 +59,8 @@ func RESTConfig(ctx context.Context, c client.Reader, cluster client.ObjectKey)
5459
return nil, errors.Wrapf(err, "failed to create REST configuration for Cluster %s/%s", cluster.Namespace, cluster.Name)
5560
}
5661

62+
restConfig.UserAgent = DefaultClusterAPIUserAgent(sourceName)
63+
restConfig.Timeout = defaultClientTimeout
64+
5765
return restConfig, nil
5866
}

controllers/remote/cluster_cache.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ import (
4242
)
4343

4444
const (
45-
defaultClientTimeout = 10 * time.Second
46-
4745
healthCheckPollInterval = 10 * time.Second
4846
healthCheckRequestTimeout = 5 * time.Second
4947
healthCheckUnhealthyThreshold = 10
48+
ClusterCacheControllerName = "cluster-cache-tracker"
5049
)
5150

5251
// ClusterCacheTracker manages client caches for workload clusters.
@@ -119,11 +118,10 @@ func (t *ClusterCacheTracker) getClusterAccessorLH(ctx context.Context, cluster
119118
// newClusterAccessor creates a new clusterAccessor.
120119
func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster client.ObjectKey) (*clusterAccessor, error) {
121120
// Get a rest config for the remote cluster
122-
config, err := RESTConfig(ctx, t.client, cluster)
121+
config, err := RESTConfig(ctx, ClusterCacheControllerName, t.client, cluster)
123122
if err != nil {
124123
return nil, errors.Wrapf(err, "error fetching REST client config for remote cluster %q", cluster.String())
125124
}
126-
config.Timeout = defaultClientTimeout
127125

128126
// Create a mapper for it
129127
mapper, err := apiutil.NewDynamicRESTMapper(config)

controllers/remote/cluster_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package remote
1818

1919
import (
2020
"testing"
21+
"time"
2122

2223
. "github.com/onsi/gomega"
2324

@@ -95,29 +96,31 @@ func TestNewClusterClient(t *testing.T) {
9596
gs := NewWithT(t)
9697

9798
client := fake.NewClientBuilder().WithScheme(testScheme).WithObjects(validSecret).Build()
98-
_, err := NewClusterClient(ctx, client, clusterWithValidKubeConfig)
99+
_, err := NewClusterClient(ctx, "test-source", client, clusterWithValidKubeConfig)
99100
// Since we do not have a remote server to connect to, we should expect to get
100101
// an error to that effect for the purpose of this test.
101102
gs.Expect(err).To(MatchError(ContainSubstring("no such host")))
102103

103-
restConfig, err := RESTConfig(ctx, client, clusterWithValidKubeConfig)
104+
restConfig, err := RESTConfig(ctx, "test-source", client, clusterWithValidKubeConfig)
104105
gs.Expect(err).NotTo(HaveOccurred())
105106
gs.Expect(restConfig.Host).To(Equal("https://test-cluster-api.nodomain.example.com:6443"))
107+
gs.Expect(restConfig.UserAgent).To(MatchRegexp("remote.test/unknown test-source (.*) cluster.x-k8s.io/unknown"))
108+
gs.Expect(restConfig.Timeout).To(Equal(10 * time.Second))
106109
})
107110

108111
t.Run("cluster with no kubeconfig", func(t *testing.T) {
109112
gs := NewWithT(t)
110113

111114
client := fake.NewClientBuilder().WithScheme(testScheme).Build()
112-
_, err := NewClusterClient(ctx, client, clusterWithNoKubeConfig)
115+
_, err := NewClusterClient(ctx, "test-source", client, clusterWithNoKubeConfig)
113116
gs.Expect(err).To(MatchError(ContainSubstring("not found")))
114117
})
115118

116119
t.Run("cluster with invalid kubeconfig", func(t *testing.T) {
117120
gs := NewWithT(t)
118121

119122
client := fake.NewClientBuilder().WithScheme(testScheme).WithObjects(invalidSecret).Build()
120-
_, err := NewClusterClient(ctx, client, clusterWithInvalidKubeConfig)
123+
_, err := NewClusterClient(ctx, "test-source", client, clusterWithInvalidKubeConfig)
121124
gs.Expect(err).To(HaveOccurred())
122125
gs.Expect(apierrors.IsNotFound(err)).To(BeFalse())
123126
})

controllers/remote/fake/cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ import (
2424

2525
// NewClusterClient returns the same client passed as input, as output. It is assumed that the client is a
2626
// fake controller-runtime client
27-
func NewClusterClient(_ context.Context, c client.Client, _ client.ObjectKey) (client.Client, error) {
27+
func NewClusterClient(_ context.Context, sourceName string, c client.Client, _ client.ObjectKey) (client.Client, error) {
2828
return c, nil
2929
}

controllers/remote/restconfig.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package remote
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
gruntime "runtime"
24+
"strings"
25+
26+
"sigs.k8s.io/cluster-api/version"
27+
)
28+
29+
const (
30+
unknowString = "unknown"
31+
)
32+
33+
func buildUserAgent(command, version, sourceName, os, arch, commit string) string {
34+
return fmt.Sprintf(
35+
"%s/%s %s (%s/%s) cluster.x-k8s.io/%s", command, version, sourceName, os, arch, commit)
36+
}
37+
38+
// DefaultClusterAPIUserAgent returns a User-Agent string built from static global vars.
39+
func DefaultClusterAPIUserAgent(sourceName string) string {
40+
return buildUserAgent(
41+
adjustCommand(os.Args[0]),
42+
adjustVersion(version.Get().GitVersion),
43+
adjustSourceName(sourceName),
44+
gruntime.GOOS,
45+
gruntime.GOARCH,
46+
adjustCommit(version.Get().GitCommit))
47+
}
48+
49+
// adjustSourceName returns the name of the source calling the client
50+
func adjustSourceName(c string) string {
51+
if len(c) == 0 {
52+
return unknowString
53+
}
54+
return c
55+
}
56+
57+
// adjustCommit returns sufficient significant figures of the commit's git hash.
58+
func adjustCommit(c string) string {
59+
if len(c) == 0 {
60+
return unknowString
61+
}
62+
if len(c) > 7 {
63+
return c[:7]
64+
}
65+
return c
66+
}
67+
68+
// adjustVersion strips "alpha", "beta", etc. from version in form
69+
// major.minor.patch-[alpha|beta|etc].
70+
func adjustVersion(v string) string {
71+
if len(v) == 0 {
72+
return unknowString
73+
}
74+
seg := strings.SplitN(v, "-", 2)
75+
return seg[0]
76+
}
77+
78+
// adjustCommand returns the last component of the
79+
// OS-specific command path for use in User-Agent.
80+
func adjustCommand(p string) string {
81+
// Unlikely, but better than returning "".
82+
if len(p) == 0 {
83+
return unknowString
84+
}
85+
return filepath.Base(p)
86+
}

controlplane/kubeadm/internal/cluster.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import (
3434
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3535
)
3636

37+
const (
38+
// KubeadmControlPlaneControllerName defines the controller used when creating clients
39+
KubeadmControlPlaneControllerName = "kubeadm-controlplane-controller"
40+
)
41+
3742
// ManagementCluster defines all behaviors necessary for something to function as a management cluster.
3843
type ManagementCluster interface {
3944
ctrlclient.Reader
@@ -86,7 +91,7 @@ func (m *Management) GetMachinesForCluster(ctx context.Context, cluster client.O
8691
func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.ObjectKey) (WorkloadCluster, error) {
8792
// TODO(chuckha): Inject this dependency.
8893
// TODO(chuckha): memoize this function. The workload client only exists as long as a reconciliation loop.
89-
restConfig, err := remote.RESTConfig(ctx, m.Client, clusterKey)
94+
restConfig, err := remote.RESTConfig(ctx, KubeadmControlPlaneControllerName, m.Client, clusterKey)
9095
if err != nil {
9196
return nil, err
9297
}

docs/book/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $(RELEASELINK): $(TOOLS_DIR)/go.mod
3636

3737
MDBOOK := $(TOOLS_BIN_DIR)/mdbook
3838
$(MDBOOK):
39-
$(CRATE_INSTALL) --git rust-lang/mdBook --tag v0.4.3 --to $(TOOLS_BIN_DIR) --force
39+
$(CRATE_INSTALL) --git rust-lang/mdBook --tag v0.4.5 --to $(TOOLS_BIN_DIR) --force
4040

4141
MDBOOK_LINKCHECK := $(TOOLS_BIN_DIR)/mdbook-linkcheck
4242
$(MDBOOK_LINKCHECK):

0 commit comments

Comments
 (0)