func()

in controller/codebase/controller.go [247:306]


func (c *Controller) pushRegistryTemplate(ctx context.Context, instance *codebaseService.Codebase) error {
	reposPath, err := PrepareControllerTempFolder(c.cnf.TempFolder, "repos")
	if err != nil {
		return fmt.Errorf("unable to create repos folder, %w", err)
	}

	privateKey, err := GetGerritPrivateKey(ctx, c.k8sClient, c.cnf)
	if err != nil {
		return fmt.Errorf("unable to get gerrit private key, %w", err)
	}

	gitService := git.Make(path.Join(reposPath, instance.Name), c.cnf.GitUsername, privateKey)
	defer func() {
		if err := gitService.Clean(); err != nil {
			c.logger.Error(err)
		}
	}()

	if err := c.initCodebaseRepo(instance, gitService); err != nil {
		return fmt.Errorf("unable to init codebase repo, %w", err)
	}

	_, err = gitService.Pull(registryRemoteName)
	if git.IsErrReferenceNotFound(err) {
		if err := c.replaceDefaultBranch(instance, gitService); err != nil {
			return fmt.Errorf("unable to replace default branch, %w", err)
		}
	} else if git.IsErrNonFastForwardUpdate(err) {
		return nil
	} else if err != nil {
		return fmt.Errorf("unable to pull, %w", err)
	}

	cachedToCommit, err := SetCachedFiles(instance.Name, c.appCache, gitService)
	if err != nil {
		return fmt.Errorf("unable to set cached files, %w", err)
	}

	valuesToCommit, err := updateRegistryValues(instance, gitService)
	if err != nil {
		return fmt.Errorf("unable to update registry values, %w", err)
	}

	if cachedToCommit || valuesToCommit {
		if err := gitService.RawCommit(&git.User{Name: instance.Annotations[registry.AnnotationCreatorUsername],
			Email: instance.Annotations[registry.AnnotationCreatorEmail]}, "set initial values.yaml from admin console"); err != nil {
			return fmt.Errorf("unable to commit values, %w", err)
		}
	}

	if err := gitService.Push(registryRemoteName, "--all"); err != nil {
		return fmt.Errorf("unable to push [all] changes to repo registry, %w", err)
	}

	if err := gitService.Push(registryRemoteName, "--tags"); err != nil {
		return fmt.Errorf("unable to push tags to repo registry, %w", err)
	}

	return nil
}