Skip to content

Commit

Permalink
Refactor pod builder for kind
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 23, 2023
1 parent e2e4602 commit 61395d3
Show file tree
Hide file tree
Showing 36 changed files with 1,680 additions and 1,852 deletions.
55 changes: 34 additions & 21 deletions pkg/kwokctl/components/kwok_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,42 @@ import (

// BuildKwokControllerComponentConfig is the configuration for building a kwok controller component.
type BuildKwokControllerComponentConfig struct {
Binary string
Image string
Version version.Version
Workdir string
BindAddress string
Port uint32
ConfigPath string
KubeconfigPath string
CaCertPath string
AdminCertPath string
AdminKeyPath string
NodeName string
Verbosity log.Level
NodeLeaseDurationSeconds uint
EnableCRDs []string
ExtraArgs []internalversion.ExtraArgs
ExtraVolumes []internalversion.Volume
ExtraEnvs []internalversion.Env
Binary string
Image string
Version version.Version
Workdir string
BindAddress string
Port uint32
ConfigPath string
KubeconfigPath string
CaCertPath string
AdminCertPath string
AdminKeyPath string
NodeIP string
NodeName string
ManageNodesWithAnnotationSelector string
Verbosity log.Level
NodeLeaseDurationSeconds uint
EnableCRDs []string
ExtraArgs []internalversion.ExtraArgs
ExtraVolumes []internalversion.Volume
ExtraEnvs []internalversion.Env
}

// BuildKwokControllerComponent builds a kwok controller component.
func BuildKwokControllerComponent(conf BuildKwokControllerComponentConfig) (component internalversion.Component) {
kwokControllerArgs := []string{
"--manage-all-nodes=true",
kwokControllerArgs := []string{}
if conf.ManageNodesWithAnnotationSelector == "" {
kwokControllerArgs = append(kwokControllerArgs,
"--manage-all-nodes=true",
)
} else {
kwokControllerArgs = append(kwokControllerArgs,
"--manage-all-nodes=false",
"--manage-nodes-with-annotation-selector="+conf.ManageNodesWithAnnotationSelector,
)
}

kwokControllerArgs = append(kwokControllerArgs, extraArgsToStrings(conf.ExtraArgs)...)

inContainer := conf.Image != ""
Expand Down Expand Up @@ -102,6 +113,7 @@ func BuildKwokControllerComponent(conf BuildKwokControllerComponentConfig) (comp
"--config=/root/.kwok/kwok.yaml",
"--tls-cert-file=/etc/kubernetes/pki/admin.crt",
"--tls-private-key-file=/etc/kubernetes/pki/admin.key",
"--node-ip="+conf.NodeIP,
"--node-name="+conf.NodeName,
"--node-port=10247",
"--server-address="+conf.BindAddress+":10247",
Expand All @@ -113,7 +125,8 @@ func BuildKwokControllerComponent(conf BuildKwokControllerComponentConfig) (comp
"--config="+conf.ConfigPath,
"--tls-cert-file="+conf.AdminCertPath,
"--tls-private-key-file="+conf.AdminKeyPath,
"--node-name=localhost",
"--node-ip="+conf.NodeIP,
"--node-name="+conf.NodeName,
"--node-port="+format.String(conf.Port),
"--server-address="+conf.BindAddress+":"+format.String(conf.Port),
"--node-lease-duration-seconds="+format.String(conf.NodeLeaseDurationSeconds),
Expand Down
166 changes: 166 additions & 0 deletions pkg/kwokctl/components/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
Copyright 2023 The Kubernetes Authors.
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 components

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/utils/format"
"sigs.k8s.io/kwok/pkg/utils/slices"
)

// ConvertToPod converts a component to a pod.
func ConvertToPod(component internalversion.Component) corev1.Pod {
env := []corev1.EnvVar{}
for _, e := range component.Envs {
env = append(env, corev1.EnvVar{
Name: e.Name,
Value: e.Value,
})
}

ports := []corev1.ContainerPort{}
for _, p := range component.Ports {
ports = append(ports, corev1.ContainerPort{
Name: p.Name,
ContainerPort: int32(p.Port),
HostPort: int32(p.Port),
Protocol: corev1.Protocol(p.Protocol),
})
}

volumes := []corev1.Volume{}
volumeMounts := []corev1.VolumeMount{}
for i, v := range component.Volumes {
name := v.Name
if name == "" {
name = fmt.Sprintf("volume-%d", i)
}
s := corev1.HostPathVolumeSource{
Path: v.HostPath,
}
if v.PathType != "" {
t := corev1.HostPathType(v.PathType)
s.Type = &t
}
volumes = append(volumes, corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
HostPath: &s,
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: name,
MountPath: v.MountPath,
ReadOnly: v.ReadOnly,
})
}

p := corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: component.Name,
Namespace: metav1.NamespaceSystem,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: component.Name,
Image: component.Image,
ImagePullPolicy: corev1.PullNever,
Args: component.Args,
Command: component.Command,
Env: env,
Ports: ports,
VolumeMounts: volumeMounts,
},
},
Volumes: volumes,
SecurityContext: &corev1.PodSecurityContext{
RunAsGroup: format.Ptr[int64](0),
RunAsUser: format.Ptr[int64](0),
},
RestartPolicy: corev1.RestartPolicyAlways,
HostNetwork: true,
},
}
return p
}

// ConvertFromPod converts a pod to a component.
func ConvertFromPod(p corev1.Pod) internalversion.Component {
if len(p.Spec.Containers) == 0 {
return internalversion.Component{}
}
ports := []internalversion.Port{}
container := p.Spec.Containers[0]

for _, port := range container.Ports {
ports = append(ports, internalversion.Port{
Name: port.Name,
Port: uint32(port.ContainerPort),
HostPort: uint32(port.HostPort),
Protocol: internalversion.Protocol(port.Protocol),
})
}

volumes := []internalversion.Volume{}

for _, vm := range container.VolumeMounts {
v, ok := slices.Find(p.Spec.Volumes, func(v corev1.Volume) bool {
return v.Name == vm.Name
})
if !ok {
continue
}
if v.HostPath == nil {
continue
}
volumes = append(volumes, internalversion.Volume{
Name: v.Name,
HostPath: v.HostPath.Path,
PathType: internalversion.HostPathType(format.ElemOrDefault(v.HostPath.Type)),
MountPath: vm.MountPath,
ReadOnly: vm.ReadOnly,
})
}

envs := []internalversion.Env{}
for _, e := range p.Spec.Containers[0].Env {
envs = append(envs, internalversion.Env{
Name: e.Name,
Value: e.Value,
})
}

return internalversion.Component{
Name: p.Name,
Image: container.Image,
Args: container.Args,
Command: container.Command,
Ports: ports,
Volumes: volumes,
Envs: envs,
}
}
5 changes: 1 addition & 4 deletions pkg/kwokctl/runtime/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ var (
InClusterKubeconfigName = "kubeconfig"
EtcdDataDirName = "etcd"
PkiName = "pki"
ManifestsName = "manifests"
Prometheus = "prometheus.yaml"
KindName = "kind.yaml"
KwokPod = "kwok-controller-pod.yaml"
DashboardDeploy = "dashboard-deployment.yaml"
PrometheusDeploy = "prometheus-deployment.yaml"
JaegerDeploy = "jaeger-deployment.yaml"
AuditPolicyName = "audit.yaml"
AuditLogName = "audit.log"
SchedulerConfigName = "scheduler.yaml"
Expand Down
Loading

0 comments on commit 61395d3

Please sign in to comment.