Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAAS-7483: Support creating OCIR repos in the non-root compartment. #642

Merged
merged 1 commit into from
Jan 20, 2022

Conversation

parthasarathy-menon
Copy link
Contributor

@parthasarathy-menon parthasarathy-menon commented Jan 17, 2022

Fn context, oracle.image-compartment-id is used to specify the compartment id to which the function image is to be deployed.

If the the repository already exists and oracle.image-compartment-id is specified, the deployment will fail if the repository is not present the same compartment id as the image-compartment-id.

There are no tests added to the code due to the lack of testing around the deploy path, in order to write a test, we would have needed to mock multiple clients and change some fundamental properties in the providers code. Instead, we have performed manual testing:

  • oracle.image-compartment-Id set in fn context
  • handling of oracle.image-compartment-Id not set (check in the code early on and skips if not set)
  • setting oracle.image-compartment-Id to COMPA with a repository/image in COMPB - code will error telling use the repo already exists in another compartment

@JadeRedworth JadeRedworth force-pushed the FAAS-7483/image-compartment-support branch 2 times, most recently from 6506436 to cbcdc51 Compare January 17, 2022 17:32
@parthasarathy-menon parthasarathy-menon force-pushed the FAAS-7483/image-compartment-support branch 2 times, most recently from 1247e42 to 0918865 Compare January 18, 2022 15:36
Comment on lines 740 to 739
func getRepositoryNamePrefix(registry string) (string, error) {
repositoryNamePrefix := ""
parts := strings.Split(registry, "/")
if len(parts) < 3 {
return "", errors.New("registry must have a dockerhub owner or private registry and repo-name")
}
for i := 2; i < len(parts); i++ {
repositoryNamePrefix += parts[i] + "/"
}
return repositoryNamePrefix[:len(repositoryNamePrefix)-1], nil
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same? Could you add some tests too, to be sure, please?

Suggested change
func getRepositoryNamePrefix(registry string) (string, error) {
repositoryNamePrefix := ""
parts := strings.Split(registry, "/")
if len(parts) < 3 {
return "", errors.New("registry must have a dockerhub owner or private registry and repo-name")
}
for i := 2; i < len(parts); i++ {
repositoryNamePrefix += parts[i] + "/"
}
return repositoryNamePrefix[:len(repositoryNamePrefix)-1], nil
}
func getRepositoryNamePrefix(registry string) (string, error) {
parts := strings.Split(registry, "/")
if len(parts) < 3 {
return "", errors.New("registry must have a dockerhub owner or private registry and repo-name")
}
return strings.Join(parts[2:], "/"), nil
}

artifactsClient.SetRegion(getRegion(oracleProvider))

// Create the repository in ImageCompartmentID
repositoryExists, err := createContainerRepositoryInCompartment(repositoryName, oracleProvider.ImageCompartmentID, artifactsClient)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the logic in this area might be more clear if it was switched around slightly?

What do you think about checking for existence of the container first, with doesRepositoryExistInComparment. If that's true, then you're done. If false, then proceed to create the repository, and if that fails, you know it's a failure that you can't continue with.

That way the response from createContainerRepositoryInCompartment doesn't have to distinguish between 409s and other responses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Updating it that way

CompartmentId: &compartmentID,
DisplayName: &repositoryName})
if err != nil {
return false, fmt.Errorf("failed to lookup Container Repository due to %s", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return false, fmt.Errorf("failed to lookup Container Repository due to %s", err)
return false, fmt.Errorf("failed to lookup Container Repository due to %w", err)

@parthasarathy-menon parthasarathy-menon force-pushed the FAAS-7483/image-compartment-support branch 2 times, most recently from 6bdc9b4 to 46dde9d Compare January 20, 2022 10:30
}
if !repositoryExists {
createContainerRepositoryInCompartment(repositoryName, oracleProvider.ImageCompartmentID, artifactsClient)
if err != nil {

This comment was marked as resolved.

This comment was marked as resolved.

if oracleProvider != nil && oracleProvider.ImageCompartmentID != "" {
// If the provider is Oracle and ImageCompartmentID is present, we need to deploy image to the ImageCompartmentID.

// Check Registry and build the repository name.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: most of these small comments aren't doing a lot for me. e.g., here "check registry and build the repository name" just says exactly what the code is doing. Personally I think it would be better (but not required) to write a small comment at the top describing roughly /why/ things are happening the way they are. e.g., "A repository must have a unique name within the tenancy. If one doesn't exist create it in the compartment; if one exists already check to see if it is in the right compartment. If it's not in the right compartment we can't continue".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the comments I am leaving are like steps and the code is what the step does. Its much easier to understand what is happening later on when reading than to see a chunk of code and figure out what it does, especially for someone new. I'll add why we do it at the top and space it so it helps in a similar way.

client/api.go Outdated
@@ -26,3 +26,7 @@ import (
func CurrentProvider() (provider.Provider, error) {
return fn_go.DefaultProviders.ProviderFromConfig(viper.GetString(config.ContextProvider), &config.ViperConfigSource{}, &provider.TerminalPassPhraseSource{})
}

func Registry() string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that Registry should be added to api.go.

if client.Registry() == "" {
return errors.New("registry name must be set, be sure to configure your context file")
}
res, err := getRepositoryNamePrefix(client.Registry())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the existing function getRepositoryName will do what you want here. Additionally it'll support name containing a different registry

Suggested change
res, err := getRepositoryNamePrefix(client.Registry())
res, err := getRepositoryName(funcfile)

@@ -697,3 +729,36 @@ func findMissingValues(signingDetails common.SigningDetails) string {
}
return strings.Join(missingValues, ",")
}

func getRepositoryNamePrefix(registry string) (string, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this fn is needed, if you use getRepositoryName as suggested elsewhere.

CompartmentId: &compartmentID,
DisplayName: &repositoryName})
if err != nil {
return false, fmt.Errorf("failed to lookup Container Repository due to %w", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return false, fmt.Errorf("failed to lookup Container Repository due to %w", err)
return false, fmt.Errorf("failed to lookup container repository due to %w", err)

if err != nil {
return false, fmt.Errorf("failed to lookup Container Repository due to %w", err)
}
if *response.RepositoryCount == 1 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this suggestion, but it seems that if the count is bigger than one it exists.

Suggested change
if *response.RepositoryCount == 1 {
if *response.RepositoryCount >= 1 {

@riconnon riconnon merged commit 1782eed into master Jan 20, 2022
@riconnon riconnon deleted the FAAS-7483/image-compartment-support branch January 20, 2022 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants