Skip to content

Commit

Permalink
Use manage to specify resources
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Sep 9, 2024
1 parent 16f3f85 commit 5ba035d
Show file tree
Hide file tree
Showing 14 changed files with 1,019 additions and 133 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/config/v1alpha1/kwok_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type KwokConfigurationOptions struct {
// is the default value for flag --tls-private-key-file
TLSPrivateKeyFile string `json:"tlsPrivateKeyFile,omitempty"`

// Manages is the option to manage an resources
Manages ManagesSelectors `json:"manages,omitempty"`

// ManageSingleNode is the option to manage a single node name.
// is the default value for flag --manage-single-node
// Note: when `manage-all-nodes` is specified as true or
Expand Down
42 changes: 42 additions & 0 deletions pkg/apis/config/v1alpha1/kwok_manage_selector_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

// ManagesSelectors holds information about the manages selectors.
type ManagesSelectors []ManagesSelector

// ManagesSelector holds information about the manages selector.
type ManagesSelector struct {
// Kind of the referent.
Kind string `json:"kind"`
// Group of the referent.
Group string `json:"group,omitempty"`
// Version of the referent.
Version string `json:"version,omitempty"`

// Name of the referent
// Only available with Node Kind.
Name string `json:"name,omitempty"`
// Labels of the referent.
// specify matched with labels.
// Only available with Node Kind.
Labels map[string]string `json:"labels,omitempty"`
// Annotations of the referent.
// specify matched with annotations.
// Only available with Node Kind.
Annotations map[string]string `json:"annotations,omitempty"`
}
59 changes: 59 additions & 0 deletions pkg/apis/config/v1alpha1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions pkg/apis/internalversion/kwok_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type KwokConfigurationOptions struct {
// TLSPrivateKeyFile is the ile containing x509 private key
TLSPrivateKeyFile string

// Manages is the option to manage the resource
Manages ManagesSelectors

// ManageSingleNode is the option to manage a single node name
ManageSingleNode string

Expand Down
247 changes: 247 additions & 0 deletions pkg/apis/internalversion/kwok_manages_selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internalversion

import (
"fmt"
"sort"
"strings"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"

"sigs.k8s.io/kwok/pkg/utils/maps"
"sigs.k8s.io/kwok/pkg/utils/slices"
)

// ManagesSelectors holds information about the manages selectors.
type ManagesSelectors []ManagesSelector

// ManagesSelector holds information about the manages selector.
type ManagesSelector struct {
// Kind of the referent.
Kind string
// Group of the referent.
Group string
// Version of the referent.
Version string

// Name of the referent
// Only available with Node Kind.
Name string
// Labels of the referent.
// specify matched with labels.
// Only available with Node Kind.
Labels map[string]string
// Annotations of the referent.
// specify matched with annotations.
// Only available with Node Kind.
Annotations map[string]string
}

func (s *ManagesSelectors) Set(sel string) error {
p, err := parseManagesSelector(sel)
if err != nil {
return err
}
*s = append(*s, *p)
return nil
}

func (s ManagesSelectors) Type() string {
return "ManagesSelectorSlice"
}

func (s ManagesSelectors) String() string {
strSlice := slices.Map(s, func(t ManagesSelector) string {
return t.String()
})
return strings.Join(strSlice, " ")
}

func (s *ManagesSelector) Set(sel string) error {
p, err := parseManagesSelector(sel)
if err != nil {
return err
}
*s = *p
return nil
}

func (s *ManagesSelector) Type() string {
return "ManagesSelector"
}

func parseManagesSelector(arg string) (*ManagesSelector, error) {
items := strings.Split(arg, ":")

t := ManagesSelector{}
gvk := items[0]
if gvk == "" {
return nil, fmt.Errorf("invalid empty target resource ref")
}

sepVersion := strings.Index(gvk, "/")
if sepVersion != -1 {
t.Version = gvk[sepVersion+1:]
gvk = gvk[:sepVersion]
}

sepGroup := strings.Index(gvk, ".")
if sepGroup != -1 {
t.Kind = gvk[:sepGroup]
t.Group = gvk[sepGroup+1:]
} else {
t.Kind = gvk
}

for _, item := range items[1:] {
sel, err := fields.ParseSelector(item)
if err != nil {
return nil, err
}
for _, req := range sel.Requirements() {
if req.Operator != selection.Equals && req.Operator != selection.DoubleEquals {
return nil, fmt.Errorf("invalid selector requirements: %s", req.Operator)
}
switch req.Field {
case "metadata.name":
t.Name = req.Value
default:
sp := strings.SplitN(req.Field, ".", 3)
if len(sp) < 2 {
return nil, fmt.Errorf("error target resource ref: %s", item)
}
if sp[0] != "metadata" {
return nil, fmt.Errorf("error target resource ref: %s", item)
}

switch sp[1] {
case "labels":
if t.Labels == nil {
t.Labels = map[string]string{}
}
t.Labels[sp[2]] = req.Value
case "annotations":
if t.Annotations == nil {
t.Annotations = map[string]string{}
}
t.Annotations[sp[2]] = req.Value
default:
return nil, fmt.Errorf("error target resource ref: %s", item)
}
}
}
}
return &t, nil
}

func (s *ManagesSelector) String() string {
if s == nil {
return ""
}

buf := &strings.Builder{}
buf.WriteString(s.Kind)
if s.Group != "" {
buf.WriteString(fmt.Sprintf(".%s", s.Group))
}
if s.Version != "" {
buf.WriteString(fmt.Sprintf("/%s", s.Version))
}
if s.Name != "" {
buf.WriteString(fmt.Sprintf(":metadata.name=%s", s.Name))
}
if len(s.Labels) > 0 {
keys := maps.Keys(s.Labels)
sort.Strings(keys)
for _, k := range keys {
buf.WriteString(fmt.Sprintf(":metadata.labels.%s=%s", k, s.Labels[k]))
}
}
if len(s.Annotations) > 0 {
keys := maps.Keys(s.Annotations)
sort.Strings(keys)
for _, k := range keys {
buf.WriteString(fmt.Sprintf(":metadata.annotations.%s=%s", k, s.Annotations[k]))
}
}
return buf.String()
}

func (s ManagesSelectors) MatchStage(stage *Stage) bool {
for _, t := range s {
if t.MatchStage(stage) {
return true
}
}
return false
}

func (s *ManagesSelector) MatchStage(stage *Stage) bool {
spec := stage.Spec
if !s.MatchResourceRef(&spec.ResourceRef) {
return false
}

if spec.Selector != nil {
if len(s.Labels) != 0 {
ml := spec.Selector.MatchLabels
for k, v := range s.Labels {
if mv, ok := ml[k]; ok && mv != v {
return false
}
}
}
if len(s.Annotations) != 0 {
ma := spec.Selector.MatchAnnotations
for k, v := range s.Annotations {
if mv, ok := ma[k]; ok && mv != v {
return false
}
}
}
}

return true
}

func (s *ManagesSelector) MatchResourceRef(ref *StageResourceRef) bool {
if s.Kind != ref.Kind {
return false
}

gv := schema.GroupVersion{
Group: s.Group,
Version: s.Version,
}
apiGroup := gv.String()
if apiGroup == "" {
apiGroup = "v1"
}

if ref.APIGroup == "" {
ref.APIGroup = "v1"
}

if apiGroup != ref.APIGroup {
return false
}

return true
}
Loading

0 comments on commit 5ba035d

Please sign in to comment.