Skip to content

Commit

Permalink
Add wait for CM in the get refs funciton
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>
  • Loading branch information
antgamdia committed Feb 2, 2022
1 parent 75b407f commit feecf0a
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package main
import (
"context"
"fmt"
"time"

ctlres "github.com/k14s/kapp/pkg/kapp/resources"
corev1 "github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
kappctrlv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
packagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
datapackagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -399,6 +402,22 @@ func (s *Server) inspectKappK8sResources(ctx context.Context, cluster, namespace
// As per https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.32.0/pkg/deploy/kapp.go#L151
appName := fmt.Sprintf("%s-ctrl", packageId)

_, dynClient, err := s.GetClients(ctx, cluster)
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to get the k8s client: '%v'", err)
}

gvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
resource := dynClient.Resource(gvr).Namespace(namespace)

// In order to be able to fetch the resources created by the kapp-controller, we need to fetch a ConfigMap
// that contains the label (the application id) used for query the k8s resources.
// We actively wait for this ConfigMap to be present in the cluster before returning the list of resources
err = WaitForResource(ctx, resource, appName, time.Second*1, time.Second*time.Duration(s.pluginConfig.timeoutSeconds))
if err != nil {
return nil, status.Errorf(codes.Internal, "timeout exceeded (%v s) waiting for resource to be installed: '%v'", s.pluginConfig.timeoutSeconds, err)
}

refs := []*corev1.ResourceRef{}

// Get the Kapp different clients
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,7 @@ func TestCreateInstalledPackage(t *testing.T) {
request *corev1.CreateInstalledPackageRequest
pluginConfig *kappControllerPluginParsedConfig
existingObjects []runtime.Object
existingTypedObjects []runtime.Object
expectedStatusCode codes.Code
expectedResponse *corev1.CreateInstalledPackageResponse
expectedPackageInstall *packagingv1alpha1.PackageInstall
Expand Down Expand Up @@ -2401,6 +2402,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -2513,6 +2529,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.Internal,
},
{
Expand Down Expand Up @@ -2607,6 +2638,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -2748,6 +2794,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -2891,6 +2952,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3035,6 +3111,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3179,6 +3270,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3319,6 +3425,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3462,6 +3583,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3605,6 +3741,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3748,6 +3899,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3891,6 +4057,21 @@ func TestCreateInstalledPackage(t *testing.T) {
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
expectedStatusCode: codes.OK,
expectedResponse: &corev1.CreateInstalledPackageResponse{
InstalledPackageRef: &corev1.InstalledPackageReference{
Expand Down Expand Up @@ -3963,7 +4144,7 @@ func TestCreateInstalledPackage(t *testing.T) {
s := Server{
pluginConfig: tc.pluginConfig,
clientGetter: func(ctx context.Context, cluster string) (clientgetter.ClientInterfaces, error) {
return clientgetter.NewClientInterfaces(typfake.NewSimpleClientset(), dynamicClient, nil), nil
return clientgetter.NewClientInterfaces(typfake.NewSimpleClientset(tc.existingTypedObjects...), dynamicClient, nil), nil
},
}

Expand Down Expand Up @@ -4497,6 +4678,20 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) {
}},
},
},
// Although it's a typical k8s object, it is retrieved with the dynamic client
&k8scorev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-ctrl",
},
Data: map[string]string{
"spec": "{\"labelKey\":\"kapp.k14s.io/app\",\"labelValue\":\"my-id\"}",
},
},
},
existingTypedObjects: []runtime.Object{
&k8scorev1.ConfigMap{
Expand Down Expand Up @@ -4541,6 +4736,7 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) {
GroupVersion: "v1",
APIResources: []metav1.APIResource{
{Name: "pods", Namespaced: true, Kind: "Pod", Verbs: []string{"list", "get"}},
{Name: "configmaps", Namespaced: true, Kind: "ConfigMap", Verbs: []string{"list", "get"}},
},
},
}
Expand All @@ -4558,7 +4754,8 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) {
{Group: datapackagingv1alpha1.SchemeGroupVersion.Group, Version: datapackagingv1alpha1.SchemeGroupVersion.Version, Resource: pkgMetadatasResource}: pkgMetadataResource + "List",
{Group: packagingv1alpha1.SchemeGroupVersion.Group, Version: packagingv1alpha1.SchemeGroupVersion.Version, Resource: pkgInstallsResource}: pkgInstallResource + "List",
// If more resources types are added, this will need to be updated accordingly
{Group: "", Version: "v1", Resource: "pods"}: "Pod" + "List",
{Group: "", Version: "v1", Resource: "pods"}: "Pod" + "List",
{Group: "", Version: "v1", Resource: "configmaps"}: "ConfigMap" + "List",
},
unstructuredObjects...,
)
Expand Down

0 comments on commit feecf0a

Please sign in to comment.