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

add tansport for rest config #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
88 changes: 49 additions & 39 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package client

import (
"net/http"
"time"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -28,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
"github.com/clusterpedia-io/client-go/constants"
"github.com/clusterpedia-io/client-go/tools/transport"
)

const (
Expand All @@ -37,46 +38,48 @@ const (
DefaultTimeoutSeconds = 10
)

var Scheme = runtime.NewScheme()

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(Scheme))
utilruntime.Must(clusterv1alpha2.AddToScheme(Scheme))
}

func Client() (client.Client, error) {
restConfig, err := ctrl.GetConfig()
config, err := ctrl.GetConfig()
if err != nil {
return nil, err
}

return newClient(restConfig)
return newClient(config)
}

func ClusterClient(cluster string) (client.Client, error) {
restConfig, err := ctrl.GetConfig()
config, err := ctrl.GetConfig()
if err != nil {
return nil, err
}

return newClient(restConfig, cluster)
return newClient(config, cluster)
}

func GetClient(restConfig *rest.Config, cluster ...string) (client.Client, error) {
return newClient(restConfig, cluster...)
func GetClient(config *rest.Config, cluster ...string) (client.Client, error) {
return newClient(config, cluster...)
}

func newClient(restConfig *rest.Config, cluster ...string) (client.Client, error) {
func newClient(config *rest.Config, cluster ...string) (client.Client, error) {
var err error

if len(cluster) == 1 {
restConfig, err = ClusterConfigFor(restConfig, cluster[0])
config, err = ClusterConfigFor(config, cluster[0])
} else {
restConfig, err = ConfigFor(restConfig)
config, err = ConfigFor(config)
}
if err != nil {
return nil, err
}

scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(clusterv1alpha2.AddToScheme(scheme))

c, err := client.New(restConfig, client.Options{
Scheme: scheme,
c, err := client.New(config, client.Options{
Scheme: Scheme,
})
if err != nil {
return nil, err
Expand All @@ -85,56 +88,63 @@ func newClient(restConfig *rest.Config, cluster ...string) (client.Client, error
return c, nil
}

func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
configShallowCopy := *cfg

// reset clusterpedia api path
if err := SetConfigDefaults(&configShallowCopy); err != nil {
func NewForConfig(cfg *rest.Config) (kubernetes.Interface, error) {
config, err := ConfigFor(cfg)
if err != nil {
return nil, err
}

return &configShallowCopy, nil
}

func ClusterConfigFor(cfg *rest.Config, cluster string) (*rest.Config, error) {
configShallowCopy, err := ConfigFor(cfg)
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
configShallowCopy.Host += constants.ClusterAPIPath + cluster
return configShallowCopy, nil

return kubeClient, nil
}

func NewForConfig(cfg *rest.Config) (kubernetes.Interface, error) {
clientConfig, err := ConfigFor(cfg)
func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface, error) {
config, err := ClusterConfigFor(cfg, cluster)
if err != nil {
return nil, err
}

kubeClient, err := kubernetes.NewForConfig(clientConfig)
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

return kubeClient, nil
}

func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface, error) {
clientConfig, err := ClusterConfigFor(cfg, cluster)
if err != nil {
func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
configShallowCopy := *cfg
if err := SetConfigDefaults(&configShallowCopy); err != nil {
return nil, err
}

kubeClient, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
// wrap a transport to rest client config
configShallowCopy.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return transport.NewTransport(configShallowCopy.Host, rt)
})

return &configShallowCopy, nil
}

func ClusterConfigFor(cfg *rest.Config, cluster string) (*rest.Config, error) {
configShallowCopy := *cfg
if err := SetConfigDefaults(&configShallowCopy); err != nil {
return nil, err
}

return kubeClient, nil
// wrap a cluster transport to rest client config
configShallowCopy.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return transport.NewTransportForCluster(configShallowCopy.Host, cluster, rt)
})

return &configShallowCopy, nil
}

func SetConfigDefaults(config *rest.Config) error {
config.Host += constants.ClusterPediaAPIPath
if config.Timeout == 0 {
config.Timeout = DefaultTimeoutSeconds * time.Second
}
Expand Down
46 changes: 6 additions & 40 deletions examples/clusterpedia-client/main.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,28 @@
/*
Copyright 2021 clusterpedia 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 main

import (
"context"
"fmt"

clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
"github.com/clusterpedia-io/client-go/tools/builder"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/clusterpedia-io/client-go/client"
)

func main() {
restConfig, err := ctrl.GetConfig()
config, err := ctrl.GetConfig()
if err != nil {
panic(err)
}
cc, err := clusterpediaclient.NewForConfig(restConfig)
client, err := client.NewClusterForConfig(config, "cluster1")
if err != nil {
panic(err)
}

collectionResource, err := cc.PediaClusterV1beta1().CollectionResource().List(context.TODO(), metav1.ListOptions{})
pod, err := client.CoreV1().Pods("default").Get(context.TODO(), "pod1", metav1.GetOptions{})
if err != nil {
panic(err)
}

for _, item := range collectionResource.Items {
fmt.Printf("resource info: %v\n", item)
}

// build listOptions
options := builder.ListOptionsBuilder().
Namespaces("kube-system").
Options()

resources, err := cc.PediaClusterV1beta1().CollectionResource().Fetch(context.TODO(), "workflows", options, nil)
if err != nil {
panic(err)
}

for _, item := range resources.Items {
fmt.Printf("resource info: %v\n", item)
}
fmt.Println(pod)
}
62 changes: 62 additions & 0 deletions examples/collectionresource-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2021 clusterpedia 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 main

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"

clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
"github.com/clusterpedia-io/client-go/tools/builder"
)

func main() {
restConfig, err := ctrl.GetConfig()
if err != nil {
panic(err)
}
cc, err := clusterpediaclient.NewForConfig(restConfig)
if err != nil {
panic(err)
}

collectionResource, err := cc.PediaClusterV1beta1().CollectionResource().List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err)
}

for _, item := range collectionResource.Items {
fmt.Printf("resource info: %v\n", item)
}

// build listOptions
options := builder.ListOptionsBuilder().
Namespaces("kube-system").
Options()

resources, err := cc.PediaClusterV1beta1().CollectionResource().Fetch(context.TODO(), "workflows", options, nil)
if err != nil {
panic(err)
}

for _, item := range resources.Items {
fmt.Printf("resource info: %v\n", item)
}
}
34 changes: 34 additions & 0 deletions examples/transport/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"fmt"
"log"
"net/http"

"github.com/clusterpedia-io/client-go/tools/transport"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)

func main() {
config, err := ctrl.GetConfig()
if err != nil {
log.Fatalf("failed to init config: %v", err)
}
config.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return transport.NewTransportForCluster(config.Host, "cluster", rt)
})

client, err := clientset.NewForConfig(config)
if err != nil {
log.Fatalf("failed to init clientset: %v", err)
}

pod, err := client.CoreV1().Pods("demo-system").Get(context.TODO(), "pod1", metav1.GetOptions{})
if err != nil {
log.Fatalf("failed to list pods: %v", err)
}
fmt.Println(pod)
}
Loading