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:55:04 UTC

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

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



##########
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:
       Thanks @zregvart, you're right. I've messed things here, and fixed it in #2821.




-- 
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