-
Notifications
You must be signed in to change notification settings - Fork 44
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: use the code way to install core-dns #579
Merged
+599
−156
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package coredns | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
"k8s.io/client-go/dynamic" | ||
clientset "k8s.io/client-go/kubernetes" | ||
|
||
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/coredns/host" | ||
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/coredns/virtualcluster" | ||
"github.com/kosmos.io/kosmos/pkg/kubenest/util" | ||
) | ||
|
||
func EnsureHostCoreDns(client clientset.Interface, name, namespace string) error { | ||
err := installCoreDnsConfigMap(client, namespace) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = EnsureCoreDnsRBAC(client, namespace, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = installCoreDnsDeployment(client, name, namespace) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func EnsureVirtualClusterCoreDns(dynamicClient dynamic.Interface, templateMapping map[string]interface{}) error { | ||
err := installCoreDnsEndpointsInVirtualCluster(dynamicClient, templateMapping) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = installCoreDnsServiceInVirtualCluster(dynamicClient, templateMapping) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func installCoreDnsDeployment(client clientset.Interface, name, namespace string) error { | ||
imageRepository, _ := util.GetImageMessage() | ||
imageTag := util.GetCoreDnsImageTag() | ||
coreDnsDeploymentBytes, err := util.ParseTemplate(host.CoreDnsDeployment, struct { | ||
Namespace, Name, ImageRepository, CoreDNSImageTag string | ||
}{ | ||
Namespace: namespace, | ||
Name: name, | ||
ImageRepository: imageRepository, | ||
CoreDNSImageTag: imageTag, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns deployment template: %w", err) | ||
} | ||
coreDnsDeployment := &appsv1.Deployment{} | ||
if err := yaml.Unmarshal([]byte(coreDnsDeploymentBytes), coreDnsDeployment); err != nil { | ||
return fmt.Errorf("error when decoding core-dns deployment: %w", err) | ||
} | ||
|
||
if err := util.CreateOrUpdateDeployment(client, coreDnsDeployment); err != nil { | ||
return fmt.Errorf("error when creating deployment for %s, err: %w", coreDnsDeployment.Name, err) | ||
} | ||
return nil | ||
} | ||
|
||
func getCoreDnsConfigMapManifest(namespace string) (*v1.ConfigMap, error) { | ||
coreDnsConfigMapBytes, err := util.ParseTemplate(host.CoreDnsCM, struct { | ||
Namespace string | ||
}{ | ||
Namespace: namespace, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error when parsing core-dns configMap template: %w", err) | ||
} | ||
|
||
config := &v1.ConfigMap{} | ||
if err := yaml.Unmarshal([]byte(coreDnsConfigMapBytes), config); err != nil { | ||
return nil, fmt.Errorf("err when decoding core-dns configMap: %w", err) | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func installCoreDnsConfigMap(client clientset.Interface, namespace string) error { | ||
config, err := getCoreDnsConfigMapManifest(namespace) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := util.CreateOrUpdateConfigMap(client, config); err != nil { | ||
return fmt.Errorf("error when creating configMap for %s, err: %w", config.Name, err) | ||
} | ||
return nil | ||
} | ||
|
||
func installCoreDnsServiceInVirtualCluster(dynamicClient dynamic.Interface, templateMapping map[string]interface{}) error { | ||
coreDnsServiceInVcBytes, err := util.ParseTemplate(virtualcluster.CoreDnsService, templateMapping) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns service in virtual cluster template: %w", err) | ||
} | ||
var obj unstructured.Unstructured | ||
if err := yaml.Unmarshal([]byte(coreDnsServiceInVcBytes), &obj); err != nil { | ||
return fmt.Errorf("err when decoding core-dns service in virtual cluster: %w", err) | ||
} | ||
|
||
err = util.CreateObject(dynamicClient, obj.GetNamespace(), obj.GetName(), &obj) | ||
if err != nil { | ||
return fmt.Errorf("error when creating core-dns service in virtual cluster err: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func installCoreDnsEndpointsInVirtualCluster(dynamicClient dynamic.Interface, templateMapping map[string]interface{}) error { | ||
coreDnsEndpointsInVcBytes, err := util.ParseTemplate(virtualcluster.CoreDnsEndpoints, templateMapping) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns service in virtual cluster template: %w", err) | ||
} | ||
var obj unstructured.Unstructured | ||
if err := yaml.Unmarshal([]byte(coreDnsEndpointsInVcBytes), &obj); err != nil { | ||
return fmt.Errorf("err when decoding core-dns service in virtual cluster: %w", err) | ||
} | ||
|
||
err = util.CreateObject(dynamicClient, obj.GetNamespace(), obj.GetName(), &obj) | ||
if err != nil { | ||
return fmt.Errorf("error when creating core-dns service in virtual cluster err: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func DeleteCoreDnsDeployment(client clientset.Interface, name, namespace string) error { | ||
// delete deployment | ||
deployName := fmt.Sprintf("%s-%s", name, "coredns") | ||
if err := util.DeleteDeployment(client, deployName, namespace); err != nil { | ||
return errors.Wrapf(err, "Failed to delete deployment %s/%s", deployName, namespace) | ||
} | ||
|
||
// delete configmap | ||
cmName := "coredns" | ||
if err := util.DeleteConfigmap(client, cmName, namespace); err != nil { | ||
return errors.Wrapf(err, "Failed to delete configmap %s/%s", cmName, namespace) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package coredns | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
clientset "k8s.io/client-go/kubernetes" | ||
|
||
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/coredns/host" | ||
"github.com/kosmos.io/kosmos/pkg/kubenest/util" | ||
) | ||
|
||
func EnsureCoreDnsRBAC(client clientset.Interface, namespace string, name string) error { | ||
if err := grantCoreDnsClusterSA(client, namespace); err != nil { | ||
return err | ||
} | ||
if err := grantCoreDnsClusterRoleBinding(client, namespace, name); err != nil { | ||
return err | ||
} | ||
if err := grantCoreDnsClusterRole(client, name); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func grantCoreDnsClusterSA(client clientset.Interface, namespace string) error { | ||
coreDnsClusterSABytes, err := util.ParseTemplate(host.CoreDnsSA, struct { | ||
Namespace string | ||
}{ | ||
Namespace: namespace, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns sa template: %w", err) | ||
} | ||
serviceAccount := &v1.ServiceAccount{} | ||
if err := yaml.Unmarshal([]byte(coreDnsClusterSABytes), serviceAccount); err != nil { | ||
return fmt.Errorf("err when decoding core-dns view Clusterrole: %w", err) | ||
} | ||
return util.CreateOrUpdateClusterSA(client, serviceAccount, namespace) | ||
} | ||
|
||
func grantCoreDnsClusterRoleBinding(client clientset.Interface, namespace string, name string) error { | ||
coreDnsClusterRoleBindingBytes, err := util.ParseTemplate(host.CoreDnsClusterRoleBinding, struct { | ||
Name string | ||
Namespace string | ||
}{ | ||
Name: name, | ||
Namespace: namespace, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns role binding template: %w", err) | ||
} | ||
viewClusterRoleBinding := &rbacv1.ClusterRoleBinding{} | ||
|
||
if err := yaml.Unmarshal([]byte(coreDnsClusterRoleBindingBytes), viewClusterRoleBinding); err != nil { | ||
return fmt.Errorf("err when decoding core-dns Clusterrole Binding: %w", err) | ||
} | ||
return util.CreateOrUpdateClusterRoleBinding(client, viewClusterRoleBinding) | ||
} | ||
|
||
func grantCoreDnsClusterRole(client clientset.Interface, name string) error { | ||
viewClusterRole := &rbacv1.ClusterRole{} | ||
coreDnsClusterRoleBytes, err := util.ParseTemplate(host.CoreDnsClusterRole, struct { | ||
Name string | ||
}{ | ||
Name: name, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error when parsing core-dns cluster role template: %w", err) | ||
} | ||
if err := yaml.Unmarshal([]byte(coreDnsClusterRoleBytes), viewClusterRole); err != nil { | ||
return fmt.Errorf("err when decoding core-dns Clusterrole: %w", err) | ||
} | ||
return util.CreateOrUpdateClusterRole(client, viewClusterRole) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
pkg/kubenest/manifest/controlplane/coredns/host/manifest_configmap.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package host | ||
|
||
const ( | ||
CoreDnsCM = ` | ||
apiVersion: v1 | ||
data: | ||
Corefile: | | ||
.:53 { | ||
errors | ||
health { | ||
lameduck 5s | ||
} | ||
ready | ||
kubernetes cluster.local in-addr.arpa ip6.arpa { | ||
pods insecure | ||
fallthrough in-addr.arpa ip6.arpa | ||
ttl 30 | ||
kubeconfig /etc/apiserver/kubeconfig | ||
} | ||
prometheus :9153 | ||
forward . /etc/resolv.conf { | ||
max_concurrent 1000 | ||
} | ||
cache 30 | ||
loop | ||
reload | ||
loadbalance | ||
} | ||
kind: ConfigMap | ||
metadata: | ||
name: coredns | ||
namespace: {{ .Namespace }} | ||
` | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be configured here