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

Fix RawExtension.Raw format #13

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Create k8s Kind Cluster
uses: helm/kind-action@v1.5.0
- name: Run test
run: go test -v ./...
5 changes: 3 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)

func decode(data []byte) (runtime.Object, *schema.GroupVersionKind, error) {
Expand Down Expand Up @@ -59,7 +58,9 @@ func stringToRawExtension(manifest string) (runtime.RawExtension, error) {
}

func objToRawExtension(obj runtime.Object) (runtime.RawExtension, error) {
serializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
// Probably RawExtension.Raw should be JSON.
// https://github.com/kubernetes/apimachinery/issues/102#issuecomment-707187760
serializer := json.NewSerializer(json.DefaultMetaFactory, wellknownScheme, wellknownScheme, false)
var buffer bytes.Buffer

err := serializer.Encode(obj, &buffer)
Expand Down
9 changes: 4 additions & 5 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
)

func TestStringToRawExtension(t *testing.T) {
Expand All @@ -33,11 +32,11 @@ metadata:
obj, _, err := decode([]byte(nsStr))
require.Nil(t, err)

_, err = objToRawExtension(obj)
rawExtension, err := objToRawExtension(obj)
require.Nil(t, err)

ns, ok := obj.(*v1.Namespace)
require.True(t, ok)
decoded, _, err := decode(rawExtension.Raw)
require.Nil(t, err)

assert.Equal(t, "test-namespace", ns.Name)
assert.Equal(t, obj, decoded)
}
115 changes: 115 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package workutils

import (
"context"
"testing"
"time"

sh "github.com/ohkinozomu/go-sh"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/pointer"
workclient "open-cluster-management.io/api/client/work/clientset/versioned"
workapiv1 "open-cluster-management.io/api/work/v1"
)

func TestE2E(t *testing.T) {
getResult, err := sh.RunR("kind get clusters")
require.Nil(t, err)
if getResult == "No kind clusters found." {
t.Log("Creating kind cluster...")
err := sh.Run("kind create cluster")
require.Nil(t, err)
}

if err := sh.Run("which clusteradm"); err != nil {
t.Log("Installing clusteradm...")
err := sh.Run("curl -L https://raw.githubusercontent.com/open-cluster-management-io/clusteradm/main/install.sh | bash")
require.Nil(t, err)
}

t.Log("Initializing cluster with clusteradm...")
err = sh.Run("clusteradm init")
require.Nil(t, err)

// hack
time.Sleep(20 * time.Second)

config, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
require.Nil(t, err)

workClientset, err := workclient.NewForConfig(config)
require.Nil(t, err)

deployment := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-deployment",
Namespace: "nginx",
Labels: map[string]string{
"app": "nginx",
},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(3),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "nginx",
},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "nginx",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: "nginx:1.14.2",
Env: []v1.EnvVar{
{
Name: "FOO",
Value: "UPDATED",
},
},
Ports: []v1.ContainerPort{
{
ContainerPort: 80,
},
},
},
},
},
},
},
}

rawExtension, err := objToRawExtension(deployment)
require.Nil(t, err)
manifestWork := &workapiv1.ManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx",
Namespace: "default",
},
Spec: workapiv1.ManifestWorkSpec{
Workload: workapiv1.ManifestsTemplate{
Manifests: []workapiv1.Manifest{
{
RawExtension: rawExtension,
},
},
},
},
}

_, err = workClientset.WorkV1().ManifestWorks("default").Create(context.Background(), manifestWork, metav1.CreateOptions{})
require.Nil(t, err)
}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/go-playground/validator/v10 v10.17.0
github.com/ohkinozomu/go-sh v0.0.1
github.com/stretchr/testify v1.8.4
k8s.io/api v0.25.5
k8s.io/apiextensions-apiserver v0.23.0
Expand All @@ -14,26 +15,45 @@ require (
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading
Loading