Skip to content

Commit

Permalink
Add partial support for DownwardsAPI with kctrl package release
Browse files Browse the repository at this point in the history
Signed-off-by: Soumik Majumder <[email protected]>
  • Loading branch information
100mik committed Jul 3, 2024
1 parent 35abe96 commit 4821a0d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cppforlife/color v1.9.1-0.20200716202919-6706ac40b835
github.com/cppforlife/go-cli-ui v0.0.0-20220520125801-e45d9169a663
github.com/getkin/kin-openapi v0.81.0
github.com/google/gnostic v0.5.7-v3refs
github.com/google/go-containerregistry v0.13.0
github.com/k14s/difflib v0.0.0-20201117154628-0c031775bf57
github.com/mitchellh/go-wordwrap v1.0.1
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.2.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/app/release/app_spec_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (b *AppSpecBuilder) Build() (kcv1alpha1.AppSpec, error) {
},
},
Spec: kcv1alpha1.AppSpec{
ServiceAccountName: "fake-sa",
Fetch: []kcv1alpha1.AppFetch{
{
// To be replaced by local fetch
Expand Down
52 changes: 51 additions & 1 deletion cli/pkg/kctrl/local/min_core_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ package local
import (
"context"

openapi_v2 "github.com/google/gnostic/openapiv2"
authenticationv1api "k8s.io/api/authentication/v1"
corev1api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
version "k8s.io/apimachinery/pkg/version"
watch "k8s.io/apimachinery/pkg/watch"
aplcorev1 "k8s.io/client-go/applyconfigurations/core/v1"
discovery "k8s.io/client-go/discovery"
Expand Down Expand Up @@ -64,6 +66,7 @@ import (
storagev1 "k8s.io/client-go/kubernetes/typed/storage/v1"
storagev1alpha1 "k8s.io/client-go/kubernetes/typed/storage/v1alpha1"
storagev1beta1 "k8s.io/client-go/kubernetes/typed/storage/v1beta1"
"k8s.io/client-go/openapi"
rest "k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -91,7 +94,7 @@ type MinCoreClient struct {

var _ kubernetes.Interface = &MinCoreClient{}

func (*MinCoreClient) Discovery() discovery.DiscoveryInterface { panic("Not implemented"); return nil }
func (*MinCoreClient) Discovery() discovery.DiscoveryInterface { return &MinDiscoveryClient{} }
func (*MinCoreClient) AdmissionregistrationV1() admissionregistrationv1.AdmissionregistrationV1Interface {
panic("Not implemented")
return nil
Expand Down Expand Up @@ -172,6 +175,11 @@ func (*MinCoreClient) CoordinationV1() coordinationv1.CoordinationV1Interface {
return nil
}
func (c *MinCoreClient) CoreV1() corev1.CoreV1Interface {
if c.client == nil {
// To be used only while releasing packages where we do not expect nil client to be used
// MinServiceAccount will mock necessary bits
return &MinCoreV1Client{nil, c.localSecrets, c.localConfigMaps}
}
return &MinCoreV1Client{c.client.CoreV1(), c.localSecrets, c.localConfigMaps}
}
func (*MinCoreClient) DiscoveryV1() discoveryv1.DiscoveryV1Interface {
Expand Down Expand Up @@ -324,6 +332,10 @@ func (*MinCoreV1Client) Services(namespace string) corev1.ServiceInterface {
return nil
}
func (c *MinCoreV1Client) ServiceAccounts(namespace string) corev1.ServiceAccountInterface {
if c.client == nil {
// To handle package release scenarios with DownwardsAPI
return &ServiceAccounts{namespace, nil}
}
return &ServiceAccounts{namespace, c.client.ServiceAccounts(namespace)}
}

Expand Down Expand Up @@ -452,6 +464,10 @@ func (*ServiceAccounts) DeleteCollection(ctx context.Context, opts metav1.Delete
return nil
}
func (sa *ServiceAccounts) Get(ctx context.Context, name string, opts metav1.GetOptions) (*corev1api.ServiceAccount, error) {
if sa.client == nil {
// To handle package release scenarios with DownwardsAPI
return &corev1api.ServiceAccount{}, nil
}
return sa.client.Get(ctx, name, opts)
}
func (*ServiceAccounts) List(ctx context.Context, opts metav1.ListOptions) (*corev1api.ServiceAccountList, error) {
Expand All @@ -471,5 +487,39 @@ func (*ServiceAccounts) Apply(ctx context.Context, serviceAccount *aplcorev1.Ser
return nil, nil
}
func (sa *ServiceAccounts) CreateToken(ctx context.Context, serviceAccountName string, tokenRequest *authenticationv1api.TokenRequest, opts metav1.CreateOptions) (*authenticationv1api.TokenRequest, error) {
if sa.client == nil {
// To handle package release scenarios with DownwardsAPI
return &authenticationv1api.TokenRequest{}, nil
}
return sa.client.CreateToken(ctx, serviceAccountName, tokenRequest, opts)
}

// Minimum required values to
type MinDiscoveryClient struct{}

var _ discovery.DiscoveryInterface = &MinDiscoveryClient{}

func (*MinDiscoveryClient) ServerVersion() (*version.Info, error) {
return &version.Info{Major: "1", Minor: "30.0-invalid", GitVersion: "1.30.0-invalid"}, nil
}

func (*MinDiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
return &metav1.APIGroupList{}, nil
}

func (*MinDiscoveryClient) RESTClient() rest.Interface { panic("Not implemented") }
func (*MinDiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
panic("Not implemented")
}
func (*MinDiscoveryClient) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
panic("Not implemented")
}
func (*MinDiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
panic("Not implemented")
}
func (*MinDiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
panic("Not implemented")
}
func (*MinDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) { panic("Not implemented") }
func (*MinDiscoveryClient) OpenAPIV3() openapi.Client { panic("Not implemented") }
func (*MinDiscoveryClient) WithLegacy() discovery.DiscoveryInterface { panic("Not implemented") }

0 comments on commit 4821a0d

Please sign in to comment.