Skip to content

Commit afebe69

Browse files
committed
Try RESTClient
RESTClient gives us the ability to access API endpoint directly. For the custom resources, we can access their API endpoint to get/update resources.
1 parent 433a8f1 commit afebe69

File tree

17 files changed

+23051
-3
lines changed

17 files changed

+23051
-3
lines changed

.openshift-tests-extension/openshift_payload_cluster-version-operator.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"source": "openshift:payload:cluster-version-operator",
99
"lifecycle": "blocking",
1010
"environmentSelector": {}
11+
},
12+
{
13+
"name": "[Jira:\"Cluster Version Operator\"] cluster-version-operator-tests should support passing tests Demo",
14+
"labels": {},
15+
"resources": {
16+
"isolation": {}
17+
},
18+
"source": "openshift:payload:cluster-version-operator",
19+
"lifecycle": "blocking",
20+
"environmentSelector": {}
1121
}
1222
]

test/cvo/cvo.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
package cvo
22

33
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
49
g "github.com/onsi/ginkgo/v2"
510
o "github.com/onsi/gomega"
11+
configv1 "github.com/openshift/api/config/v1"
12+
13+
"github.com/openshift/cluster-version-operator/test/utilities"
614
)
715

816
var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator-tests should support passing tests`, func() {
917
g.It("the sanity test should pass", func() {
1018
o.Expect(true).To(o.BeTrue())
1119
})
20+
21+
g.It("Demo", func() {
22+
23+
result := &configv1.ClusterVersion{}
24+
apiPath := "/apis/config.openshift.io/v1/clusterversions/version"
25+
26+
client := utilities.MustGetRestClient(configv1.GroupVersion.Group, configv1.GroupVersion.Version, apiPath)
27+
err := client.
28+
Get().
29+
AbsPath(apiPath).
30+
Namespace("default").
31+
// Resource("clusterversion").
32+
// Name("version").
33+
Do(context.TODO()).Into(result)
34+
35+
o.Expect(err).NotTo(o.HaveOccurred(), err.Error())
36+
37+
jsonData, err := json.MarshalIndent(result, "", " ")
38+
if err != nil {
39+
log.Fatalf("Error marshaling to JSON: %v", err)
40+
}
41+
42+
// Print the JSON data as a string
43+
fmt.Printf("Successfully retrieved data: %v", string(jsonData))
44+
})
1245
})

test/utilities/connection.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/openshift/client-go/build/clientset/versioned/scheme"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/runtime/schema"
811
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/rest"
913
"k8s.io/client-go/tools/clientcmd"
1014
)
1115

12-
// getKubeClient creates a Kubernetes clientset.
13-
func getKubeClient() (*kubernetes.Clientset, error) {
16+
func getClientConfig() (*rest.Config, error) {
1417
configPath, present := os.LookupEnv("KUBECONFIG")
1518
if !present {
1619
return nil, errors.New("the environment variable KUBECONFIG must be set")
1720
}
1821
config, err := clientcmd.BuildConfigFromFlags("", configPath)
22+
return config, err
23+
}
24+
25+
// getKubeClient creates a Kubernetes clientset.
26+
func getKubeClient() (*kubernetes.Clientset, error) {
27+
config, err := getClientConfig()
1928
if err != nil {
2029
return nil, fmt.Errorf("unable to load build config: %w", err)
2130
}
@@ -24,14 +33,44 @@ func getKubeClient() (*kubernetes.Clientset, error) {
2433
if err != nil {
2534
return nil, fmt.Errorf("unable to create a Kubernetes clientset: %w", err)
2635
}
36+
2737
return clientset, nil
2838
}
2939

30-
// MustGetKubeClient creates a Kubernetes clientset, or panics on failures.
40+
// getRestClient creates a Kubernetes RESTClient instance, or panics on failures.
41+
// RESTClient is more powerfull than clientset which provides access to
42+
// the Kubernetes API as a set of basic REST primitives (GET, POST, PUT, DELETE).
43+
func getRestClient(groupName string, groupVersion string, apiPath string) (*rest.RESTClient, error) {
44+
config, err := getClientConfig()
45+
if err != nil {
46+
return nil, fmt.Errorf("unable to load build config: %w", err)
47+
}
48+
49+
config.APIPath = apiPath
50+
config.GroupVersion = &schema.GroupVersion{Group: groupName, Version: groupVersion}
51+
config.ContentType = runtime.ContentTypeJSON
52+
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
53+
54+
restClient, err := rest.RESTClientFor(config)
55+
return restClient, err
56+
}
57+
58+
// MustGetKubeClient creates a Kubernetes clientset instance, or panics on failures.
3159
func MustGetKubeClient() *kubernetes.Clientset {
3260
clientset, err := getKubeClient()
3361
if err != nil {
3462
panic("unable to create a Kubernetes clientset: " + err.Error())
3563
}
3664
return clientset
3765
}
66+
67+
// MustGetRestClient creates a Kubernetes RESTClient instance, or panics on failures.
68+
// RESTClient is more powerfull than clientset which provides access to
69+
// the Kubernetes API as a set of basic REST primitives (GET, POST, PUT, DELETE).
70+
func MustGetRestClient(groupName string, groupVersion string, apiPath string) *rest.RESTClient {
71+
clientset, err := getRestClient(groupName, groupVersion, apiPath)
72+
if err != nil {
73+
panic("unable to create a RESTClient instance: " + err.Error())
74+
}
75+
return clientset
76+
}

test/utilities/deepcopy.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package utilities
2+
3+
import "k8s.io/apimachinery/pkg/runtime"
4+
5+
// DeepCopyInto copies all properties of this object into another object of the
6+
// same type that is provided as a pointer.
7+
func (in *Resourse) DeepCopyInto(out *Resourse) {
8+
out.TypeMeta = in.TypeMeta
9+
out.ObjectMeta = in.ObjectMeta
10+
out.Spec = in.Spec
11+
out.Status = in.Status
12+
}
13+
14+
// DeepCopyObject returns a generically typed copy of an object
15+
func (in *Resourse) DeepCopyObject() runtime.Object {
16+
out := Resourse{}
17+
in.DeepCopyInto(&out)
18+
19+
return &out
20+
}
21+
22+
func (in *ResourseList) DeepCopyObject() runtime.Object {
23+
out := ResourseList{}
24+
if in.Items != nil {
25+
out.Items = make([]Resourse, len(in.Items))
26+
for i := range in.Items {
27+
in.Items[i].DeepCopyInto(&out.Items[i])
28+
}
29+
}
30+
31+
return &out
32+
}

test/utilities/object.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package utilities
2+
3+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4+
5+
type Resourse struct {
6+
metav1.TypeMeta `json:",inline"`
7+
8+
metav1.ObjectMeta `json:"metadata,omitempty"`
9+
10+
Spec map[string]any `json:"spec"`
11+
12+
Status map[string]any `json:"status"`
13+
}
14+
15+
type ResourseList struct {
16+
metav1.TypeMeta `json:",inline"`
17+
Items []Resourse `json:"items"`
18+
}

vendor/github.com/openshift/api/build/v1/consts.go

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

0 commit comments

Comments
 (0)