You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/12/08 09:18:39 UTC

[GitHub] [camel-k] zregvart commented on a change in pull request #2814: feat: Faster operator startup

zregvart commented on a change in pull request #2814:
URL: https://github.com/apache/camel-k/pull/2814#discussion_r764678151



##########
File path: pkg/install/kamelets.go
##########
@@ -58,60 +68,123 @@ func KameletCatalog(ctx context.Context, c client.Client, namespace string) erro
 		return fmt.Errorf("kamelet directory %q is a file", kameletDir)
 	}
 
-	files, err := ioutil.ReadDir(kameletDir)
+	g, gCtx := errgroup.WithContext(ctx)
+
+	err = filepath.WalkDir(kameletDir, func(p string, f fs.DirEntry, err error) error {
+		if err != nil {
+			return err
+		}
+		if f.IsDir() && f.Name() != d.Name() {
+			return fs.SkipDir
+		}
+		if !(strings.HasSuffix(f.Name(), ".yaml") || strings.HasSuffix(f.Name(), ".yml")) {
+			return nil
+		}
+		// We may want to throttle the creation of Go routines if the number of bundled Kamelets increases.
+		g.Go(func() error {
+			return applyKamelet(gCtx, c, path.Join(kameletDir, f.Name()), namespace)
+		})
+		return nil
+	})
 	if err != nil {
 		return err
 	}
 
-	for _, file := range files {
-		if file.IsDir() || !(strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml")) {
-			continue
-		}
+	return g.Wait()
+}
 
-		content, err := util.ReadFile(path.Join(kameletDir, file.Name()))
-		if err != nil {
-			return err
-		}
+func applyKamelet(ctx context.Context, c client.Client, path string, namespace string) error {
+	content, err := util.ReadFile(path)
+	if err != nil {
+		return err
+	}
 
-		obj, err := kubernetes.LoadResourceFromYaml(c.GetScheme(), string(content))
-		if err != nil {
-			return err
-		}
-		if k, ok := obj.(*v1alpha1.Kamelet); ok {
-			existing := &v1alpha1.Kamelet{}
-			err = c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: k.Name}, existing)
-			if err != nil {
-				if k8serrors.IsNotFound(err) {
-					existing = nil
-				} else {
-					return err
-				}
-			}
-
-			if existing == nil || existing.Labels[v1alpha1.KameletBundledLabel] == "true" {
-				if k.GetAnnotations() == nil {
-					k.SetAnnotations(make(map[string]string))
-				}
-				k.GetAnnotations()[kamelVersionAnnotation] = defaults.Version
-
-				if k.GetLabels() == nil {
-					k.SetLabels(make(map[string]string))
-				}
-				k.GetLabels()[v1alpha1.KameletBundledLabel] = "true"
-				k.GetLabels()[v1alpha1.KameletReadOnlyLabel] = "true"
-
-				err := ObjectOrCollect(ctx, c, namespace, nil, true, k)
-				if err != nil {
-					return errors.Wrapf(err, "could not create resource from file %q", path.Join(kameletDir, file.Name()))
-				}
-			}
+	obj, err := kubernetes.LoadResourceFromYaml(c.GetScheme(), string(content))
+	if err != nil {
+		return err
+	}
+	kamelet, ok := obj.(*v1alpha1.Kamelet)
+	if !ok {
+		return fmt.Errorf("cannot load Kamelet from file %q", path)
+	}
+
+	kamelet.Namespace = namespace
+
+	if kamelet.GetAnnotations() == nil {
+		kamelet.SetAnnotations(make(map[string]string))
+	}
+	kamelet.GetAnnotations()[kamelVersionAnnotation] = defaults.Version
 
+	if kamelet.GetLabels() == nil {
+		kamelet.SetLabels(make(map[string]string))
+	}
+	kamelet.GetLabels()[v1alpha1.KameletBundledLabel] = "true"
+	kamelet.GetLabels()[v1alpha1.KameletReadOnlyLabel] = "true"
+
+	if hasServerSideApply {
+		err := serverSideApply(ctx, c, kamelet)
+		switch {
+		case err == nil:
+			break
+		case isIncompatibleServerError(err):
+			hasServerSideApply = false

Review comment:
       Could this be a logic error? Looks like first Kamelet creation that can't be server-side applied is skipped. Should this be:
   
   ```suggestion
   			hasServerSideApply = false
   			clientSideApply(ctx, c, kamelet)
   			break
   ```

##########
File path: pkg/install/kamelets.go
##########
@@ -19,30 +19,40 @@ package install
 
 import (
 	"context"
+	"errors"
 	"fmt"
-	"io/ioutil"
+	"io/fs"
+	"net/http"
 	"os"
 	"path"
+	"path/filepath"
 	"strings"
 
-	"github.com/apache/camel-k/pkg/util"
+	"golang.org/x/sync/errgroup"
 
-	"github.com/pkg/errors"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
+	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+	"k8s.io/apimachinery/pkg/runtime"
 	"k8s.io/apimachinery/pkg/types"
 
+	ctrl "sigs.k8s.io/controller-runtime/pkg/client"
+
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/client"
+	"github.com/apache/camel-k/pkg/util"
 	"github.com/apache/camel-k/pkg/util/defaults"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
+	"github.com/apache/camel-k/pkg/util/patch"
 )
 
 const (
 	kameletDirEnv     = "KAMELET_CATALOG_DIR"
 	defaultKameletDir = "/kamelets/"
 )
 
-// KameletCatalog installs the bundled KameletCatalog into one namespace.
+var hasServerSideApply = true

Review comment:
       Should this be `atomic.Value`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org