Skip to content

Commit

Permalink
feat: make cluster and namespace level ConvertPolicy available
Browse files Browse the repository at this point in the history
Signed-off-by: ONE7live <[email protected]>
  • Loading branch information
ONE7live committed Mar 22, 2024
1 parent d7a183e commit dedfbbb
Show file tree
Hide file tree
Showing 21 changed files with 3,522 additions and 2,501 deletions.
1,406 changes: 1,406 additions & 0 deletions deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml

Large diffs are not rendered by default.

1,255 changes: 0 additions & 1,255 deletions deploy/crds/kosmos.io_podconversions.yaml

This file was deleted.

2,469 changes: 1,250 additions & 1,219 deletions deploy/crds/kosmos.io_podconvertpolicies.yaml

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions pkg/apis/kosmos/v1alpha1/clusterpodconvertpolicy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +genclient:nonNamespaced
// +kubebuilder:resource:scope="Cluster"
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=cpcp
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type ClusterPodConvertPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec is the specification for the behaviour of the ClusterPodConvertPolicy.
// +required
Spec ClusterPodConvertPolicySpec `json:"spec"`
}

type ClusterPodConvertPolicySpec struct {
// A label query over a set of resources.
// If name is not empty, labelSelector will be ignored.
// +required
LabelSelector metav1.LabelSelector `json:"labelSelector"`

// A label query over a set of resources.
// If name is not empty, LeafNodeSelector will be ignored.
// +option
LeafNodeSelector *metav1.LabelSelector `json:"leafNodeSelector,omitempty"`

// Converters are some converter for convert pod when pod synced from root cluster to leaf cluster
// pod will use these converters to scheduled in leaf cluster
// +optional
Converters *Converters `json:"converters,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type ClusterPodConvertPolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []ClusterPodConvertPolicy `json:"items"`
}
18 changes: 16 additions & 2 deletions pkg/apis/kosmos/v1alpha1/podconvertpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
)

// +genclient
// +kubebuilder:resource:scope="Namespaced"
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=pc;pcs
// +kubebuilder:resource:shortName=pcp
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type PodConvertPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec is the specification for the behaviour of the podConversion.
// Spec is the specification for the behaviour of the PodConvertPolicy.
// +required
Spec PodConvertPolicySpec `json:"spec"`
}
Expand Down Expand Up @@ -50,6 +51,8 @@ type Converters struct {
AffinityConverter *AffinityConverter `json:"affinityConverter,omitempty"`
// +optional
TopologySpreadConstraintsConverter *TopologySpreadConstraintsConverter `json:"topologySpreadConstraintsConverter,omitempty"`
// +optional
HostAliasesConverter *HostAliasesConverter `json:"hostAliasesConverter,omitempty"`
}

// ConvertType if the operation type when convert pod from root cluster to leaf cluster.
Expand Down Expand Up @@ -125,6 +128,17 @@ type TopologySpreadConstraintsConverter struct {
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
}

// HostAliasesConverter is an optional list of hosts and IPs that will be injected into the pod's hosts file if specified.
// This is only valid for non-hostNetwork pods.
type HostAliasesConverter struct {
// +kubebuilder:validation:Enum=add;remove;replace
// +required
ConvertType ConvertType `json:"convertType"`

// +optional
HostAliases []corev1.HostAlias `json:"hostAliases,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type PodConvertPolicyList struct {
Expand Down
115 changes: 115 additions & 0 deletions pkg/apis/kosmos/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 2 additions & 0 deletions pkg/apis/kosmos/v1alpha1/zz_generated.register.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -800,14 +800,21 @@ func (r *RootPodReconciler) createVolumes(ctx context.Context, lr *leafUtils.Lea
func (r *RootPodReconciler) mutatePod(ctx context.Context, pod *corev1.Pod, nodeName string) error {
klog.V(4).Infof("Converting pod %v/%+v", pod.Namespace, pod.Name)

podConvertPolicyList := &kosmosv1alpha1.PodConvertPolicyList{}
err := r.Client.List(ctx, podConvertPolicyList, &client.ListOptions{
cpcpList := &kosmosv1alpha1.ClusterPodConvertPolicyList{}
err := r.Client.List(ctx, cpcpList, &client.ListOptions{})
if err != nil {
return fmt.Errorf("list cluster pod convert policy error: %v", err)
}

pcpList := &kosmosv1alpha1.PodConvertPolicyList{}
err = r.Client.List(ctx, pcpList, &client.ListOptions{
Namespace: pod.Namespace,
})
if err != nil {
return fmt.Errorf("list convert policy error: %v", err)
return fmt.Errorf("list pod convert policy error: %v", err)
}
if len(podConvertPolicyList.Items) <= 0 {

if len(cpcpList.Items) <= 0 && len(pcpList.Items) <= 0 {
// no matched policy, skip
return nil
}
Expand All @@ -818,11 +825,20 @@ func (r *RootPodReconciler) mutatePod(ctx context.Context, pod *corev1.Pod, node
return fmt.Errorf("get node error: %v, nodeName: %s", err, pod.Spec.NodeName)
}

matchedPolicy, err := convertpolicy.GetMatchPodConvertPolicy(*podConvertPolicyList, pod.Labels, rootNode.Labels)
if err != nil {
return fmt.Errorf("get convert policy error: %v", err)
if len(cpcpList.Items) > 0 {
matchedPolicy, err := convertpolicy.GetMatchClusterPodConvertPolicy(*cpcpList, pod.Labels, rootNode.Labels)
if err != nil {
return fmt.Errorf("get pod convert policy error: %v", err)
}
podutils.ConvertPod(pod, matchedPolicy, nil)
} else {
matchedPolicy, err := convertpolicy.GetMatchPodConvertPolicy(*pcpList, pod.Labels, rootNode.Labels)
if err != nil {
return fmt.Errorf("get pod convert policy error: %v", err)
}
podutils.ConvertPod(pod, nil, matchedPolicy)
}
podutils.ConvertPod(pod, matchedPolicy)

klog.V(4).Infof("Convert pod %v/%+v success", pod.Namespace, pod.Name)
return nil
}
Expand Down
Loading

0 comments on commit dedfbbb

Please sign in to comment.