Skip to content

Commit

Permalink
Merge pull request kosmos-io#539 from duanmengkk/0.3.2
Browse files Browse the repository at this point in the history
lift some pr from 0.3.0 to main
  • Loading branch information
duanmengkk authored May 11, 2024
2 parents 71c767f + 17ccab9 commit 9311810
Show file tree
Hide file tree
Showing 13 changed files with 3,134 additions and 1,297 deletions.
36 changes: 34 additions & 2 deletions deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spec:
listKind: ClusterPodConvertPolicyList
plural: clusterpodconvertpolicies
shortNames:
- pc
- cpcp
singular: clusterpodconvertpolicy
scope: Namespaced
versions:
Expand All @@ -34,7 +34,7 @@ spec:
metadata:
type: object
spec:
description: Spec is the specification for the behaviour of the PodConvertPolicy.
description: Spec is the specification for the behaviour of the ClusterPodConvertPolicy.
properties:
converters:
description: Converters are some converter for convert pod when pod
Expand Down Expand Up @@ -958,6 +958,38 @@ spec:
required:
- convertType
type: object
hostAliasesConverter:
description: 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.
properties:
convertType:
description: ConvertType if the operation type when convert
pod from root cluster to leaf cluster.
enum:
- add
- remove
- replace
type: string
hostAliases:
items:
description: HostAlias holds the mapping between IP and
hostnames that will be injected as an entry in the pod's
hosts file.
properties:
hostnames:
description: Hostnames for the above IP address.
items:
type: string
type: array
ip:
description: IP address of the host file entry.
type: string
type: object
type: array
required:
- convertType
type: object
nodeNameConverter:
description: NodeNameConverter used to modify the pod's nodeName
when pod synced to leaf cluster
Expand Down
1,255 changes: 0 additions & 1,255 deletions deploy/crds/kosmos.io_podconversions.yaml

This file was deleted.

34 changes: 33 additions & 1 deletion deploy/crds/kosmos.io_podconvertpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spec:
listKind: PodConvertPolicyList
plural: podconvertpolicies
shortNames:
- pc
- pcp
singular: podconvertpolicy
scope: Namespaced
versions:
Expand Down Expand Up @@ -958,6 +958,38 @@ spec:
required:
- convertType
type: object
hostAliasesConverter:
description: 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.
properties:
convertType:
description: ConvertType if the operation type when convert
pod from root cluster to leaf cluster.
enum:
- add
- remove
- replace
type: string
hostAliases:
items:
description: HostAlias holds the mapping between IP and
hostnames that will be injected as an entry in the pod's
hosts file.
properties:
hostnames:
description: Hostnames for the above IP address.
items:
type: string
type: array
ip:
description: IP address of the host file entry.
type: string
type: object
type: array
required:
- convertType
type: object
nodeNameConverter:
description: NodeNameConverter used to modify the pod's nodeName
when pod synced to leaf cluster
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/kosmos/v1alpha1/clusterpodconvertpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
// +genclient:nonNamespaced
// +kubebuilder:resource:scope="Cluster"
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=pc
// +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 PodConvertPolicy.
// Spec is the specification for the behaviour of the ClusterPodConvertPolicy.
// +required
Spec ClusterPodConvertPolicySpec `json:"spec"`
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/apis/kosmos/v1alpha1/podconvertpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

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

type PodConvertPolicy struct {
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
28 changes: 28 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.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package pod

import (
"context"
"time"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -85,9 +87,28 @@ func NewRootDeleteOption(pod *corev1.Pod) client.DeleteOption {
}

func NewLeafDeleteOption(pod *corev1.Pod) client.DeleteOption {
gracePeriodSeconds := new(int64)
if pod.DeletionGracePeriodSeconds != nil {
gracePeriodSeconds = pod.DeletionGracePeriodSeconds
var gracePeriodSeconds *int64
// Check if DeletionTimestamp is set and before the current time
current := metav1.NewTime(time.Now())
if pod.DeletionTimestamp != nil && pod.DeletionTimestamp.Before(&current) {
//force
gracePeriodSeconds = new(int64)
} else {
if pod.DeletionGracePeriodSeconds != nil {
//DeletionGracePeriodSeconds determines how long it will take before the pod status changes to Termination
//wait
gracePeriodSeconds = pod.DeletionGracePeriodSeconds
} else if pod.Spec.TerminationGracePeriodSeconds != nil {
//TerminationGracePeriodSeconds is how long to wait after the pod is marked as Termination state before the container process is forcibly deleted.
//wait
gracePeriodSeconds = pod.Spec.TerminationGracePeriodSeconds
}

if pod.DeletionGracePeriodSeconds != nil && pod.Spec.TerminationGracePeriodSeconds != nil {
//Sum both grace periods
totalGracePeriod := *pod.DeletionGracePeriodSeconds + *pod.Spec.TerminationGracePeriodSeconds
gracePeriodSeconds = &totalGracePeriod
}
}

return &rootDeleteOption{
Expand Down Expand Up @@ -163,8 +184,3 @@ func (r *LeafPodReconciler) SetupWithManager(mgr manager.Manager) error {
})).
Complete(r)
}

// func ShouldSkipStatusUpdate(pod *corev1.Pod) bool {
// return pod.Status.Phase == corev1.PodSucceeded ||
// pod.Status.Phase == corev1.PodFailed
// }
Original file line number Diff line number Diff line change
Expand Up @@ -825,14 +825,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{
Namespace: pod.Namespace,
})
if err != nil {
return fmt.Errorf("list convert policy error: %v", err)
cpcpList := &kosmosv1alpha1.ClusterPodConvertPolicyList{}
pcpList := &kosmosv1alpha1.PodConvertPolicyList{}
err := r.Client.List(ctx, cpcpList, &client.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Infof("list cluster pod convert policy error: %v", err)
} else {
err = r.Client.List(ctx, pcpList, &client.ListOptions{
Namespace: pod.Namespace,
})
if err != nil && !errors.IsNotFound(err) {
klog.Infof("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 @@ -843,11 +850,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
46 changes: 44 additions & 2 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.

Loading

0 comments on commit 9311810

Please sign in to comment.