Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support load config from stdin #684

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -46,11 +46,19 @@ func loadRawMessages(src []string) ([]json.RawMessage, error) {
var raws []json.RawMessage

for _, p := range src {
if p == "-" {
r, err := loadRaw(os.Stdin)
if err != nil {
return nil, err
}
raws = append(raws, r...)
continue
}
p, err := path.Expand(p)
if err != nil {
return nil, err
}
r, err := loadRawConfig(p)
r, err := loadRawFromFile(p)
if err != nil {
return nil, err
}
@@ -426,19 +434,23 @@ func FilterWithoutTypeFromContext[T InternalObject](ctx context.Context) (out []
return FilterWithoutType[T](objs)
}

func loadRawConfig(p string) ([]json.RawMessage, error) {
var raws []json.RawMessage
func loadRawFromFile(p string) ([]json.RawMessage, error) {
file, err := os.Open(p)
if err != nil {
return nil, err
}
defer func() {
_ = file.Close()
}()
decoder := utilyaml.NewYAMLToJSONDecoder(file)
return loadRaw(file)
}

func loadRaw(r io.Reader) ([]json.RawMessage, error) {
var raws []json.RawMessage
decoder := utilyaml.NewYAMLToJSONDecoder(r)
for {
var raw json.RawMessage
err = decoder.Decode(&raw)
err := decoder.Decode(&raw)
if err != nil {
if errors.Is(err, io.EOF) {
break
16 changes: 5 additions & 11 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ limitations under the License.
package config

import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"reflect"
"testing"
@@ -62,7 +62,7 @@ func TestConfig(t *testing.T) {
}
}

func Test_loadRawConfig(t *testing.T) {
func Test_loadRaw(t *testing.T) {
tests := []struct {
name string
data []byte
@@ -142,19 +142,13 @@ kind: KwokctlConfiguration
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := filepath.Join(t.TempDir(), "config.yaml")
err := os.WriteFile(p, tt.data, 0640)
if err != nil {
t.Fatal(err)
}

got, err := loadRawConfig(p)
got, err := loadRaw(bytes.NewBuffer(tt.data))
if (err != nil) != tt.wantErr {
t.Errorf("loadRawConfig() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("loadRaw() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadRawConfig() got = %v, want %v", got, tt.want)
t.Errorf("loadRaw() got = %v, want %v", got, tt.want)
}
})
}
4 changes: 4 additions & 0 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@ func InitFlags(ctx context.Context, flags *pflag.FlagSet) (context.Context, erro
}
configPaths := make([]string, 0, len(*config))
for _, c := range *config {
if c == "-" {
configPaths = append(configPaths, c)
continue
}
configPath, err := path.Expand(c)
if err != nil {
return nil, err
63 changes: 0 additions & 63 deletions test/kwokctl/kwokctl-config-patches.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions test/kwokctl/kwokctl-config-runtimes.yaml

This file was deleted.

12 changes: 10 additions & 2 deletions test/kwokctl/kwokctl_auto_detect.test.sh
Original file line number Diff line number Diff line change
@@ -15,12 +15,20 @@

DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")

source "${DIR}/helper.sh"
source "${DIR}/suite.sh"

function main() {
local all_releases=("${@}")
for release in "${all_releases[@]}"; do
KWOK_KUBE_VERSION="${release}" kwokctl -v=-4 create cluster --timeout 30m --wait 30m --quiet-pull --config "${DIR}"/kwokctl-config-runtimes.yaml || exit 1
name="test-kwokctl-auto-detect-${release}"
create_cluster "${name}" "${release}" --config - <<EOF
apiVersion: config.kwok.x-k8s.io/v1alpha1
kind: KwokctlConfiguration
options:
runtimes:
- none
- binary
EOF
kwokctl delete cluster || exit 1
done
}
68 changes: 67 additions & 1 deletion test/kwokctl/kwokctl_extra_test.sh
Original file line number Diff line number Diff line change
@@ -76,7 +76,73 @@ function main() {
echo "------------------------------"
echo "Testing extra on ${KWOK_RUNTIME} for ${release}"
name="cluster-${KWOK_RUNTIME}-${release//./-}"
create_cluster "${name}" "${release}" --config "${DIR}/kwokctl-config-patches.yaml" --prometheus-port 9090
create_cluster "${name}" "${release}" --config - <<EOF
apiVersion: config.kwok.x-k8s.io/v1alpha1
kind: KwokctlConfiguration
options:
prometheusPort: 9090
componentsPatches:
- name: kube-apiserver
extraArgs:
- key: v
value: "5"
extraVolumes:
- name: tmp-apiserver
hostPath: ./extras/apiserver
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
- name: kube-controller-manager
extraArgs:
- key: v
value: "5"
extraVolumes:
- name: tmp-controller-manager
hostPath: ./extras/controller-manager
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
- name: kube-scheduler
extraArgs:
- key: v
value: "5"
extraVolumes:
- name: tmp-scheduler
hostPath: ./extras/scheduler
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
- name: kwok-controller
extraArgs:
- key: v
value: "-4"
extraVolumes:
- name: tmp-controller
hostPath: ./extras/controller
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
- name: etcd
extraArgs:
- key: log-level
value: "debug"
extraVolumes:
- name: tmp-etcd
hostPath: ./extras/etcd
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
- name: prometheus
extraArgs:
- key: log.level
value: "debug"
extraVolumes:
- name: tmp-prometheus
hostPath: ./extras/prometheus
mountPath: /extras/tmp
readOnly: false
pathType: DirectoryOrCreate
EOF
test_prometheus || failed+=("prometheus_${name}")
delete_cluster "${name}"
done