Skip to content

Commit 19efdc8

Browse files
Add initial support for daemonsets (#217)
Signed-off-by: galal-hussein <[email protected]>
1 parent 54be0ba commit 19efdc8

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

k3k-kubelet/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
type config struct {
1212
ClusterName string `yaml:"clusterName,omitempty"`
1313
ClusterNamespace string `yaml:"clusterNamespace,omitempty"`
14-
NodeName string `yaml:"nodeName,omitempty"`
14+
ServiceName string `yaml:"serviceName,omitempty"`
1515
Token string `yaml:"token,omitempty"`
1616
AgentHostname string `yaml:"agentHostname,omitempty"`
1717
HostConfigPath string `yaml:"hostConfigPath,omitempty"`
@@ -46,8 +46,8 @@ func (c *config) unmarshalYAML(data []byte) error {
4646
if c.AgentHostname == "" {
4747
c.AgentHostname = conf.AgentHostname
4848
}
49-
if c.NodeName == "" {
50-
c.NodeName = conf.NodeName
49+
if c.ServiceName == "" {
50+
c.ServiceName = conf.ServiceName
5151
}
5252
if c.Token == "" {
5353
c.Token = conf.Token

k3k-kubelet/controller/webhook/pod.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type webhookHandler struct {
3333
client ctrlruntimeclient.Client
3434
scheme *runtime.Scheme
3535
nodeName string
36+
serviceName string
3637
clusterName string
3738
clusterNamespace string
3839
logger *log.Logger
@@ -41,11 +42,12 @@ type webhookHandler struct {
4142
// AddPodMutatorWebhook will add a mutator webhook to the virtual cluster to
4243
// modify the nodeName of the created pods with the name of the virtual kubelet node name
4344
// as well as remove any status fields of the downward apis env fields
44-
func AddPodMutatorWebhook(ctx context.Context, mgr manager.Manager, hostClient ctrlruntimeclient.Client, clusterName, clusterNamespace, nodeName string, logger *log.Logger) error {
45+
func AddPodMutatorWebhook(ctx context.Context, mgr manager.Manager, hostClient ctrlruntimeclient.Client, clusterName, clusterNamespace, nodeName, serviceName string, logger *log.Logger) error {
4546
handler := webhookHandler{
4647
client: mgr.GetClient(),
4748
scheme: mgr.GetScheme(),
4849
logger: logger,
50+
serviceName: serviceName,
4951
clusterName: clusterName,
5052
clusterNamespace: clusterNamespace,
5153
nodeName: nodeName,
@@ -107,7 +109,7 @@ func (w *webhookHandler) configuration(ctx context.Context, hostClient ctrlrunti
107109
if !ok {
108110
return nil, errors.New("webhook CABundle does not exist in secret")
109111
}
110-
webhookURL := "https://" + w.nodeName + ":" + webhookPort + webhookPath
112+
webhookURL := "https://" + w.serviceName + ":" + webhookPort + webhookPath
111113
return &admissionregistrationv1.MutatingWebhookConfiguration{
112114
TypeMeta: metav1.TypeMeta{
113115
APIVersion: "admissionregistration.k8s.io/v1",

k3k-kubelet/kubelet.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func newKubelet(ctx context.Context, c *config, logger *k3klog.Logger) (*kubelet
127127
return nil, errors.New("unable to create controller-runtime mgr for virtual cluster: " + err.Error())
128128
}
129129
logger.Info("adding pod mutator webhook")
130-
if err := k3kwebhook.AddPodMutatorWebhook(ctx, virtualMgr, hostClient, c.ClusterName, c.ClusterNamespace, c.NodeName, logger); err != nil {
130+
if err := k3kwebhook.AddPodMutatorWebhook(ctx, virtualMgr, hostClient, c.ClusterName, c.ClusterNamespace, c.AgentHostname, c.ServiceName, logger); err != nil {
131131
return nil, errors.New("unable to add pod mutator webhook for virtual cluster: " + err.Error())
132132
}
133133

@@ -141,7 +141,7 @@ func newKubelet(ctx context.Context, c *config, logger *k3klog.Logger) (*kubelet
141141
return nil, errors.New("failed to add pvc syncer controller: " + err.Error())
142142
}
143143

144-
clusterIP, err := clusterIP(ctx, c.AgentHostname, c.ClusterNamespace, hostClient)
144+
clusterIP, err := clusterIP(ctx, c.ServiceName, c.ClusterNamespace, hostClient)
145145
if err != nil {
146146
return nil, errors.New("failed to extract the clusterIP for the server service: " + err.Error())
147147
}
@@ -161,7 +161,7 @@ func newKubelet(ctx context.Context, c *config, logger *k3klog.Logger) (*kubelet
161161
return &kubelet{
162162
virtualCluster: virtualCluster,
163163

164-
name: c.NodeName,
164+
name: c.AgentHostname,
165165
hostConfig: hostConfig,
166166
hostClient: hostClient,
167167
virtConfig: virtConfig,

k3k-kubelet/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func main() {
6161
EnvVars: []string{"SERVER_PORT"},
6262
Value: "10250",
6363
},
64+
&cli.StringFlag{
65+
Name: "service-name",
66+
Usage: "The service name deployed by the k3k controller",
67+
Destination: &cfg.ServiceName,
68+
EnvVars: []string{"SERVICE_NAME"},
69+
},
6470
&cli.StringFlag{
6571
Name: "agent-hostname",
6672
Usage: "Agent Hostname used for TLS SAN for the kubelet server",

pkg/controller/cluster/agent/shared.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewSharedAgent(cluster *v1alpha1.Cluster, serviceIP, image, imagePullPolicy
4444
}
4545

4646
func (s *SharedAgent) Config() ctrlruntimeclient.Object {
47-
config := sharedAgentData(s.cluster, s.token, s.Name(), s.serviceIP)
47+
config := sharedAgentData(s.cluster, s.Name(), s.token, s.serviceIP)
4848

4949
return &v1.Secret{
5050
TypeMeta: metav1.TypeMeta{
@@ -61,19 +61,18 @@ func (s *SharedAgent) Config() ctrlruntimeclient.Object {
6161
}
6262
}
6363

64-
func sharedAgentData(cluster *v1alpha1.Cluster, token, nodeName, ip string) string {
64+
func sharedAgentData(cluster *v1alpha1.Cluster, serviceName, token, ip string) string {
6565
version := cluster.Spec.Version
6666
if cluster.Spec.Version == "" {
6767
version = cluster.Status.HostVersion
6868
}
6969
return fmt.Sprintf(`clusterName: %s
7070
clusterNamespace: %s
71-
nodeName: %s
72-
agentHostname: %s
7371
serverIP: %s
72+
serviceName: %s
7473
token: %s
7574
version: %s`,
76-
cluster.Name, cluster.Namespace, nodeName, nodeName, ip, token, version)
75+
cluster.Name, cluster.Namespace, ip, serviceName, token, version)
7776
}
7877

7978
func (s *SharedAgent) Resources() ([]ctrlruntimeclient.Object, error) {
@@ -178,6 +177,17 @@ func (s *SharedAgent) podSpec() v1.PodSpec {
178177
"--config",
179178
sharedKubeletConfigPath,
180179
},
180+
Env: []v1.EnvVar{
181+
{
182+
Name: "AGENT_HOSTNAME",
183+
ValueFrom: &v1.EnvVarSource{
184+
FieldRef: &v1.ObjectFieldSelector{
185+
APIVersion: "v1",
186+
FieldPath: "spec.nodeName",
187+
},
188+
},
189+
},
190+
},
181191
VolumeMounts: []v1.VolumeMount{
182192
{
183193
Name: "config",

0 commit comments

Comments
 (0)