Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support install corends to virtual cluster #698

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ cmd/kubenest/node-agent/cert.pem
cmd/kubenest/node-agent/key.pem
cmd/kubenest/node-agent/agent.env
hack/k8s-in-k8s/nodes.txt
develop
develop

cmd/kubenest/node-agent/app/client/app.log
3 changes: 3 additions & 0 deletions deploy/crds/kosmos.io_kubenestconfigurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ spec:
type: string
type: array
type: object
useTenantDns:
default: false
type: boolean
type: object
kubeNestType:
type: string
Expand Down
3 changes: 3 additions & 0 deletions deploy/crds/kosmos.io_virtualclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ spec:
type: string
type: array
type: object
useTenantDns:
default: false
type: boolean
type: object
kubeconfig:
description: Kubeconfig is the kubeconfig of the virtual kubernetes's
Expand Down
1 change: 1 addition & 0 deletions deploy/virtual-cluster-components-manifest-cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ data:
{"name": "kube-proxy", "path": "/kosmos/manifest/kube-proxy/*.yaml"},
{"name": "calico", "path": "/kosmos/manifest/calico/*.yaml"},
{"name": "keepalived", "path": "/kosmos/manifest/keepalived/*.yaml"},
{"name": "core-dns-tenant", "path": "/kosmos/manifest/core-dns/tenant/*.yaml"},
]
host-core-dns-components: |
[
Expand Down
2 changes: 1 addition & 1 deletion hack/k8s-in-k8s/g.env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PATH_KUBELET_CONF=.
KUBELET_CONFIG_NAME=
HOST_CORE_DNS=10.96.0.10
# kubeadm switch
USE_KUBEADM=true
USE_KUBEADM=false
# Generate kubelet.conf TIMEOUT
KUBELET_CONF_TIMEOUT=30

Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/kosmos/v1alpha1/kubenestconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ type KubeInKubeConfig struct {
// +kubebuilder:default=hostNetwork
// +optional
ApiServerServiceType ApiServerServiceType `yaml:"apiServerServiceType" json:"apiServerServiceType,omitempty"`

// +kubebuilder:default=false
// +optional
UseTenantDns bool `yaml:"useTenantDns" json:"useTenantDns,omitempty"`
}

// TenantEntrypoint contains the configuration for the tenant entrypoint.
Expand Down
6 changes: 6 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pkg/kubenest/constants/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ const (
// core-dns
KubeDNSSVCName = "kube-dns"
// nolint
HostCoreDnsComponents = "host-core-dns-components"
VirtualCoreDnsComponents = "virtual-core-dns-components"
PrometheusRuleManifest = "prometheus-rules"
HostCoreDnsComponents = "host-core-dns-components"
VirtualCoreDnsComponents = "virtual-core-dns-components"
PrometheusRuleManifest = "prometheus-rules"
TenantCoreDnsComponentName = "core-dns-tenant"

StateLabelKey = "kosmos-io/state"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (e *CoreDNSController) Reconcile(ctx context.Context, request reconcile.Req
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

if targetVirtualCluster.Spec.KubeInKubeConfig != nil && targetVirtualCluster.Spec.KubeInKubeConfig.UseTenantDns {
return reconcile.Result{}, nil
}

// Get the corresponding svc
var kubesvc v1.Service
if err := e.Get(ctx, request.NamespacedName, &kubesvc); err != nil {
Expand Down
33 changes: 17 additions & 16 deletions pkg/kubenest/controller/virtualcluster_init_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,29 @@ func TestCreateApiAnpServer(t *testing.T) {
if len(apiAnpAgentSvc.Spec.Ports) != 4 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports len != 4")
}
if apiAnpAgentSvc.Spec.Ports[0].Name != "agentport" {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[0].Name != agentport")
ports := make([]int32, 5)
for _, port := range apiAnpAgentSvc.Spec.Ports {
v, ok := nameMap[port.Name]
if ok {
ports[v] = port.Port
} else {
t.Fatalf("can not get node port for %s", port.Name)
}
}
if apiAnpAgentSvc.Spec.Ports[0].Port != 8081 {

if ports[1] != 8081 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[0].Port != 8081")
}
if apiAnpAgentSvc.Spec.Ports[1].Name != "serverport" {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[1].Name != serverport")
}
if apiAnpAgentSvc.Spec.Ports[1].Port != 8082 {

if ports[2] != 8082 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[1].Port != 8082")
}
if apiAnpAgentSvc.Spec.Ports[2].Name != "healthport" {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[2].Name != healthport")
}
if apiAnpAgentSvc.Spec.Ports[2].Port != 8083 {

if ports[3] != 8083 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[2].Port != 8083")
}
if apiAnpAgentSvc.Spec.Ports[3].Name != "adminport" {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[3].Name != adminport")
}
if apiAnpAgentSvc.Spec.Ports[3].Port != 8084 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[3].Port != 8084")

if ports[4] != 8084 {
t.Fatalf("apiAnpAgentSvc.Spec.Ports[2].Port != 8084")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func ParseServerTemplate(apiServerServiceSubnet string) (*corev1.Service, error)
ServiceName, Namespace, ServiceType string
ServicePort int32
IPFamilies []corev1.IPFamily
UseApiServerNodePort bool
}{
ServiceName: fmt.Sprintf("%s-%s", "test", "apiserver"),
Namespace: "test-namespace",
ServiceType: constants.ApiServerServiceType,
ServicePort: 40010,
IPFamilies: ipFamilies,
ServiceName: fmt.Sprintf("%s-%s", "test", "apiserver"),
Namespace: "test-namespace",
ServiceType: constants.ApiServerServiceType,
ServicePort: 40010,
IPFamilies: ipFamilies,
UseApiServerNodePort: false,
})

if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/kubenest/tasks/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewCoreDNSTask() workflow.Task {
return workflow.Task{
Name: "coreDns",
Run: runCoreDns,
Skip: skipCoreDns,
RunSubTasks: true,
Tasks: []workflow.Task{
{
Expand All @@ -46,6 +47,19 @@ func NewCoreDNSTask() workflow.Task {
}
}

func skipCoreDns(d workflow.RunData) (bool, error) {
data, ok := d.(InitData)
if !ok {
return false, errors.New("coreDns task invoked with an invalid data struct")
}

vc := data.VirtualCluster()
if vc.Spec.KubeInKubeConfig != nil && vc.Spec.KubeInKubeConfig.UseTenantDns {
return true, nil
}
return false, nil
}

func runCoreDns(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
Expand Down
31 changes: 29 additions & 2 deletions pkg/kubenest/tasks/manifests_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type ComponentConfig struct {
Path string `json:"path" yaml:"path"`
}

type SkipComponentCondition struct {
Condition bool
ComponentName string
}

func NewComponentsFromManifestsTask() workflow.Task {
return workflow.Task{
Name: "manifests-components",
Expand All @@ -53,6 +58,14 @@ func runComponentsFromManifests(r workflow.RunData) error {
return nil
}

func getSkipComponentsForVirtualCluster(condition []*SkipComponentCondition) map[string]bool {
skipComponents := map[string]bool{}
for _, c := range condition {
skipComponents[c.ComponentName] = c.Condition
}
return skipComponents
}

func applyComponentsManifests(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
Expand Down Expand Up @@ -96,10 +109,24 @@ func applyComponentsManifests(r workflow.RunData) error {
templatedMapping["KeepalivedReplicas"] = keepalivedReplicas
}

UseTenantDns := data.VirtualCluster().Spec.KubeInKubeConfig != nil && data.VirtualCluster().Spec.KubeInKubeConfig.UseTenantDns

skipComponents := getSkipComponentsForVirtualCluster([]*SkipComponentCondition{
{
// skip coredns component if tenant dns is enabled
Condition: !UseTenantDns,
ComponentName: constants.TenantCoreDnsComponentName,
}, {
// skip keepalived component if vip is not enabled
Condition: !keepalivedEnable,
ComponentName: constants.VipKeepalivedComponentName,
},
})

for _, component := range components {
klog.V(2).Infof("Deploy component %s", component.Name)
// skip keepalived component if vip is not enabled
if !keepalivedEnable && component.Name == constants.VipKeepalivedComponentName {
if v, ok := skipComponents[component.Name]; ok && v {
klog.V(2).Infof("Deploy component %s skipped", component.Name)
continue
}
err = applyTemplatedManifests(component.Name, dynamicClient, component.Path, templatedMapping)
Expand Down
Loading
Loading