func Install()

in pkg/util/olm/operator.go [147:230]


func Install(ctx context.Context, client client.Client, namespace string, global bool, options Options, collection *kubernetes.Collection,
	tolerations []string, nodeSelectors []string, resourcesRequirements []string, envVars []string) (bool, error) {
	options, err := fillDefaults(options, client)
	if err != nil {
		return false, err
	}
	if installed, err := IsOperatorInstalled(ctx, client, namespace, global, options); err != nil {
		return false, err
	} else if installed {
		// Already installed
		return false, nil
	}

	targetNamespace := namespace
	if global {
		targetNamespace = options.GlobalNamespace
	}

	sub := operatorsv1alpha1.Subscription{
		ObjectMeta: v1.ObjectMeta{
			Name:      options.Package,
			Namespace: targetNamespace,
		},
		Spec: &operatorsv1alpha1.SubscriptionSpec{
			CatalogSource:          options.Source,
			CatalogSourceNamespace: options.SourceNamespace,
			Package:                options.Package,
			Channel:                options.Channel,
			StartingCSV:            options.StartingCSV,
			InstallPlanApproval:    operatorsv1alpha1.ApprovalAutomatic,
			Config:                 &operatorsv1alpha1.SubscriptionConfig{},
		},
	}
	// Additional configuration
	err = maybeSetTolerations(&sub, tolerations)
	if err != nil {
		return false, fmt.Errorf("could not set tolerations: %w", err)
	}
	err = maybeSetNodeSelectors(&sub, nodeSelectors)
	if err != nil {
		return false, fmt.Errorf("could not set node selectors: %w", err)
	}
	err = maybeSetResourcesRequirements(&sub, resourcesRequirements)
	if err != nil {
		return false, fmt.Errorf("could not set resources requirements: %w", err)
	}
	err = maybeSetEnvVars(&sub, envVars)
	if err != nil {
		return false, fmt.Errorf("could not set environment variables: %w", err)
	}

	if collection != nil {
		collection.Add(&sub)
	} else if err := client.Create(ctx, &sub); err != nil {
		return false, err
	}

	if !global {
		group, err := findOperatorGroup(ctx, client, namespace)
		if err != nil {
			return false, err
		}
		if group == nil {
			group = &operatorsv1.OperatorGroup{
				ObjectMeta: v1.ObjectMeta{
					Namespace:    namespace,
					GenerateName: fmt.Sprintf("%s-", namespace),
				},
				Spec: operatorsv1.OperatorGroupSpec{
					TargetNamespaces: []string{namespace},
				},
			}
			if collection != nil {
				collection.Add(group)
			} else if err := client.Create(ctx, group); err != nil {
				return false, fmt.Errorf("namespace %s has no operator group defined and "+
					"current user is not able to create it. "+
					"Make sure you have the right roles to install operators from OLM"+": %w", namespace, err)

			}
		}
	}
	return true, nil
}