Skip to content

Commit

Permalink
Merge pull request #277 from wzshiming/fix/stages
Browse files Browse the repository at this point in the history
Fix default stages name
  • Loading branch information
wzshiming authored Feb 11, 2023
2 parents 6c0518c + c2b3c3d commit dab81b5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/kwok/controllers/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,28 @@ import (
"k8s.io/apimachinery/pkg/util/yaml"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/apis/v1alpha1"
"sigs.k8s.io/kwok/pkg/utils/expression"
"sigs.k8s.io/kwok/pkg/utils/format"
)

// NewStagesFromYaml returns stages from yaml data.
func NewStagesFromYaml(data []byte) ([]*internalversion.Stage, error) {
var podStageStatus []*internalversion.Stage
var stages []*internalversion.Stage
decoder := yaml.NewYAMLToJSONDecoder(bytes.NewBuffer(data))
for {
var stage internalversion.Stage
var stage v1alpha1.Stage
err := decoder.Decode(&stage)
if errors.Is(err, io.EOF) {
break
}
podStageStatus = append(podStageStatus, &stage)
internalStage, err := internalversion.ConvertToInternalVersionStage(&stage)
if err != nil {
return nil, err
}
stages = append(stages, internalStage)
}
return podStageStatus, nil
return stages, nil
}

// NewLifecycle returns a new Lifecycle.
Expand Down
78 changes: 78 additions & 0 deletions pkg/kwok/controllers/lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2023 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 controllers

import (
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
)

func TestNewStagesFromYaml(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want []*internalversion.Stage
wantErr bool
}{
{
name: "empty",
args: args{
data: []byte(`kind: Stage
apiVersion: kwok.x-k8s.io/v1alpha1
metadata:
name: node-test
spec:
resourceRef:
apiGroup: v1
kind: Node`),
},
want: []*internalversion.Stage{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node-test",
},
Spec: internalversion.StageSpec{
ResourceRef: internalversion.StageResourceRef{
APIGroup: "v1",
Kind: "Node",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewStagesFromYaml(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("NewStagesFromYaml() error = %v, wantErr %v", err, tt.wantErr)
return
}

if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("NewStagesFromYaml() diff (-got +want): %s", diff)
}
})
}
}

0 comments on commit dab81b5

Please sign in to comment.