Skip to content

Commit 28b9650

Browse files
Allow Multiple Cluster Pod Creation Using Distributed Petset (#17)
Signed-off-by: souravbiswassanto <[email protected]>
1 parent 57bdeb2 commit 28b9650

File tree

102 files changed

+17677
-6523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+17677
-6523
lines changed

apis/apps/v1/constants.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
const (
20+
ManifestWorkRoleLabel = "open-cluster-management.io/role"
21+
ManifestWorkClusterNameLabel = "open-cluster-management.io/cluster-name"
22+
RolePod = "pod"
23+
RolePVC = "pvc"
24+
)

apis/apps/v1/petset_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ type PetSet struct {
6262

6363
// A PetSetSpec is the specification of a PetSet.
6464
type PetSetSpec struct {
65+
// Distributed means manifestworks will be used to manage pods
66+
Distributed bool `json:"distributed,omitempty"`
67+
6568
// replicas is the desired number of replicas of the given Template.
6669
// These are replicas in the sense that they are instantiations of the
6770
// same Template, but individual replicas also have a consistent identity.

apis/apps/v1/placementpolicy_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ type PlacementPolicySpec struct {
6363
// If specified, the pod's scheduling constraints
6464
// +optional
6565
Affinity *Affinity `json:"affinity,omitempty"`
66+
67+
// OCM provides spec for distributed pod placements using open cluster management
68+
// +optional
69+
OCM *OCMSpec `json:"ocm,omitempty"`
70+
}
71+
72+
type OCMSpec struct {
73+
DistributionRules []DistributionRule `json:"distributionRules,omitempty"`
74+
SliceName string `json:"sliceName,omitempty"`
75+
}
76+
77+
type DistributionRule struct {
78+
ClusterName string `json:"clusterName,omitempty"`
79+
Replicas []int32 `json:"replicas,omitempty"`
6680
}
6781

6882
type ZoneSpreadConstraint struct {

apis/apps/v1/zz_generated.deepcopy.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/listers/apps/v1/petset_expansion.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ import (
2424
v1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/labels"
27+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28+
"k8s.io/klog/v2"
29+
apiworkv1 "open-cluster-management.io/api/work/v1"
2730
)
2831

2932
// PetSetListerExpansion allows custom methods to be added to
3033
// PetSetLister.
3134
type PetSetListerExpansion interface {
3235
GetPodPetSets(pod *v1.Pod) ([]*api.PetSet, error)
36+
GetManifestWorkPetSets(mw *apiworkv1.ManifestWork) ([]*api.PetSet, error)
3337
}
3438

3539
// PetSetNamespaceListerExpansion allows custom methods to be added to
@@ -77,3 +81,47 @@ func (s *petSetLister) GetPodPetSets(pod *v1.Pod) ([]*api.PetSet, error) {
7781

7882
return psList, nil
7983
}
84+
85+
// GetManifestWorkPetSets returns a list of PetSets that potentially match a manifestwork.
86+
// It lists PetSets across all namespaces and matches them based on labels.
87+
func (s *petSetLister) GetManifestWorkPetSets(mw *apiworkv1.ManifestWork) ([]*api.PetSet, error) {
88+
if len(mw.Labels) == 0 {
89+
return nil, fmt.Errorf("no PetSets found for manifestwork %s because it has no labels", mw.Name)
90+
}
91+
92+
list, err := s.List(labels.Everything())
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var psList []*api.PetSet
98+
for _, ps := range list {
99+
selector, err := metav1.LabelSelectorAsSelector(ps.Spec.Selector)
100+
if err != nil {
101+
klog.Warningf("PetSet %s/%s has an invalid selector: %v", ps.Namespace, ps.Name, err)
102+
continue
103+
}
104+
105+
if selector.Empty() || !selector.Matches(labels.Set(mw.Labels)) {
106+
continue
107+
}
108+
psList = append(psList, ps)
109+
}
110+
111+
if len(psList) == 0 {
112+
return nil, fmt.Errorf("could not find any PetSet for manifestwork %v/%s in any namespace with labels: %v", mw.Namespace, mw.Name, mw.Labels)
113+
}
114+
115+
if len(psList) > 1 {
116+
setNames := []string{}
117+
for _, s := range psList {
118+
setNames = append(setNames, s.Name)
119+
}
120+
utilruntime.HandleError(
121+
fmt.Errorf(
122+
"user error: more than one PetSet is selecting manifestwork with labels: %+v. Sets: %v",
123+
mw.Labels, setNames))
124+
}
125+
126+
return psList, nil
127+
}

crds/apps.k8s.appscode.com_petsets.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ spec:
4646
spec:
4747
description: Spec defines the desired identities of pods in this set.
4848
properties:
49+
distributed:
50+
description: Distributed means manifestworks will be used to manage
51+
pods
52+
type: boolean
4953
minReadySeconds:
5054
description: |-
5155
Minimum number of seconds for which a newly created pod should be ready

crds/apps.k8s.appscode.com_placementpolicies.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ spec:
100100
- maxSkew
101101
- whenUnsatisfiable
102102
type: object
103+
ocm:
104+
description: OCM provides spec for distributed pod placements using
105+
open cluster management
106+
properties:
107+
distributionRules:
108+
items:
109+
properties:
110+
clusterName:
111+
type: string
112+
replicas:
113+
items:
114+
format: int32
115+
type: integer
116+
type: array
117+
type: object
118+
type: array
119+
sliceName:
120+
type: string
121+
type: object
103122
zoneSpreadConstraint:
104123
properties:
105124
maxSkew:

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
module kubeops.dev/petset
22

3-
go 1.23.0
3+
go 1.23.6
44

55
toolchain go1.24.0
66

77
require (
88
github.com/google/cel-go v0.22.0
99
github.com/google/go-cmp v0.7.0
1010
github.com/google/gofuzz v1.2.0
11+
github.com/pkg/errors v0.9.1
1112
github.com/spf13/cobra v1.9.1
1213
github.com/spf13/pflag v1.0.6
1314
github.com/stretchr/testify v1.10.0
1415
gomodules.xyz/logs v0.0.7
1516
gomodules.xyz/pointer v0.1.0
1617
gomodules.xyz/x v0.0.17
1718
k8s.io/api v0.32.2
19+
k8s.io/apiextensions-apiserver v0.32.2
1820
k8s.io/apimachinery v0.32.2
1921
k8s.io/apiserver v0.32.2
2022
k8s.io/client-go v0.32.2
@@ -25,6 +27,7 @@ require (
2527
k8s.io/utils v0.0.0-20241210054802-24370beab758
2628
kmodules.xyz/client-go v0.32.1
2729
kmodules.xyz/go-containerregistry v0.0.14
30+
open-cluster-management.io/api v1.0.0
2831
sigs.k8s.io/controller-runtime v0.20.3
2932
sigs.k8s.io/yaml v1.4.0
3033
)
@@ -112,7 +115,6 @@ require (
112115
github.com/onsi/gomega v1.36.2 // indirect
113116
github.com/opencontainers/go-digest v1.0.0 // indirect
114117
github.com/opencontainers/image-spec v1.1.0 // indirect
115-
github.com/pkg/errors v0.9.1 // indirect
116118
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
117119
github.com/prometheus/client_golang v1.20.5 // indirect
118120
github.com/prometheus/client_model v0.6.1 // indirect
@@ -154,6 +156,7 @@ require (
154156
gomodules.xyz/clock v0.0.0-20200817085942-06523dba733f // indirect
155157
gomodules.xyz/flags v0.1.3 // indirect
156158
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
159+
gomodules.xyz/mergo v0.3.13 // indirect
157160
gomodules.xyz/sets v0.2.1 // indirect
158161
gomodules.xyz/wait v0.2.0 // indirect
159162
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 // indirect
@@ -162,9 +165,7 @@ require (
162165
google.golang.org/protobuf v1.36.3 // indirect
163166
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
164167
gopkg.in/inf.v0 v0.9.1 // indirect
165-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
166168
gopkg.in/yaml.v3 v3.0.1 // indirect
167-
k8s.io/apiextensions-apiserver v0.32.2 // indirect
168169
k8s.io/cloud-provider v0.32.2 // indirect
169170
k8s.io/component-helpers v0.32.2 // indirect
170171
k8s.io/controller-manager v0.32.2 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
257257
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
258258
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
259259
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
260+
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
261+
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
260262
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
261263
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
262264
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -643,6 +645,8 @@ gomodules.xyz/jsonpatch/v2 v2.5.0 h1:JELs8RLM12qJGXU4u/TO3V25KW8GreMKl9pdkk14RM0
643645
gomodules.xyz/jsonpatch/v2 v2.5.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
644646
gomodules.xyz/logs v0.0.7 h1:dkhpdQuzj+pOS3S7VaOq+JV7BVU7f68/k3uDYufhPow=
645647
gomodules.xyz/logs v0.0.7/go.mod h1:IEIZbRl9zua2jb35NU4KoqxUEDPmKvem3PhfRHqQI54=
648+
gomodules.xyz/mergo v0.3.13 h1:q6cL/MMXZH/MrR2+yjSihFFq6UifXqjwaqI48B6cMEM=
649+
gomodules.xyz/mergo v0.3.13/go.mod h1:F/2rKC7j0URTnHUKDiTiLcGdLMhdv8jK2Za3cRTUVmc=
646650
gomodules.xyz/pointer v0.1.0 h1:sG2UKrYVSo6E3r4itAjXfPfe4fuXMi0KdyTHpR3vGCg=
647651
gomodules.xyz/pointer v0.1.0/go.mod h1:sPLsC0+yLTRecUiC5yVlyvXhZ6LAGojNCRWNNqoplvo=
648652
gomodules.xyz/sets v0.2.0/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
@@ -706,6 +710,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
706710
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
707711
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
708712
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
713+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
709714
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
710715
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
711716
gotest.tools/v3 v3.1.0 h1:rVV8Tcg/8jHUkPUorwjaMTtemIMVXfIPKiOqnhEhakk=
@@ -745,6 +750,8 @@ kmodules.xyz/client-go v0.32.1 h1:W8TWGBBaokQEqfesbdhlRFMMV0TChcRbMRQ0eTIRS88=
745750
kmodules.xyz/client-go v0.32.1/go.mod h1:tqe40/iDtO/RSNaDJs/a+rgnMPSOcK622HpOmOFvcds=
746751
kmodules.xyz/go-containerregistry v0.0.14 h1:8MgLFa74HymAJEyjH7fyQJn5u2Ok6qPPFQX8ARfcXp0=
747752
kmodules.xyz/go-containerregistry v0.0.14/go.mod h1:xz0iGC3noyMi5NNAzXWTH6KqfiIgFWZAomw+U2zVOXs=
753+
open-cluster-management.io/api v1.0.0 h1:54QllH9DTudCk6VrGt0q8CDsE3MghqJeTaTN4RHZpE0=
754+
open-cluster-management.io/api v1.0.0/go.mod h1:/OeqXycNBZQoe3WG6ghuWsMgsKGuMZrK8ZpsU6gWL0Y=
748755
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
749756
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 h1:CPT0ExVicCzcpeN4baWEV2ko2Z/AsiZgEdwgcfwLgMo=
750757
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0/go.mod h1:Ve9uj1L+deCXFrPOk1LpFXqTg7LCFzFso6PA48q/XZw=

0 commit comments

Comments
 (0)