This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #668
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,6 @@ | |
[[constraint]] | ||
name = "github.com/docker/go" | ||
version = "1.5.1-1" | ||
|
||
[[constraint]] | ||
name = "k8s.io/apimachinery" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright (c) 2019-Present Pivotal Software, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
|
||
"github.com/deislabs/duffle/pkg/loader" | ||
) | ||
|
||
const relocateDesc = ` | ||
Relocates the images referenced by a bundle and creates a new bundle with an updated image map. | ||
The --repository-prefix flag determines the repositories for the relocated images. | ||
Each image is tagged with a name starting with the given prefix and pushed to the repository. | ||
For example, if the repository-prefix is example.com/user, the image istio/proxyv2 is relocated | ||
to a name starting with example.com/user/ and pushed to a repository hosted by example.com. | ||
` | ||
|
||
type relocateCmd struct { | ||
bundle string | ||
dest string | ||
repoPrefix string | ||
out io.Writer | ||
//verbose bool | ||
//insecure bool | ||
} | ||
|
||
func newRelocateCmd(w io.Writer) *cobra.Command { | ||
relocate := &relocateCmd{out: w} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "relocate [INPUT-BUNDLE] [OUTPUT-BUNDLE]", | ||
Short: "relocate images in a CNAB bundle", | ||
Long: relocateDesc, | ||
Example: `duffle relocate helloworld hellorelocated --repository-prefix example.com/user`, | ||
Args: cobra.ExactArgs(2), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
// validate --repository-prefix if it is set, otherwise allow flag omission to be diagnosed as such | ||
if cmd.Flags().Changed("repository-prefix") { | ||
if err := flagValidRepository("repository-prefix")(cmd); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
relocate.bundle = args[0] | ||
relocate.dest = args[1] | ||
|
||
return relocate.run() | ||
}, | ||
} | ||
|
||
f := cmd.Flags() | ||
f.StringVarP(&relocate.repoPrefix, "repository-prefix", "r", "", "a prefix for relocated image names") | ||
cmd.MarkFlagRequired("repository-prefix") | ||
//f.BoolVarP(&relocate.verbose, "verbose", "v", false, "Verbose output") | ||
//f.BoolVarP(&relocate.insecure, "insecure", "k", false, "Do not verify the bundle (INSECURE)") | ||
|
||
return cmd | ||
} | ||
|
||
func (ex *relocateCmd) run() error { | ||
bundlefile, l, err := ex.setup() | ||
if err != nil { | ||
return err | ||
} | ||
if err := ex.Relocate(bundlefile, l); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ex *relocateCmd) Relocate(bundlefile string, l loader.Loader) error { | ||
return nil | ||
} | ||
|
||
func (ex *relocateCmd) setup() (string, loader.Loader, error) { | ||
//bundlefile, err := resolveBundleFilePath(ex.bundle, ex.home.String(), ex.bundleIsFile, ex.insecure) | ||
//if err != nil { | ||
// return "", nil, err | ||
//} | ||
// | ||
//l, err := getLoader(ex.home.String(), ex.insecure) | ||
//if err != nil { | ||
// return "", nil, err | ||
//} | ||
|
||
return "", nil, nil | ||
} | ||
|
||
type flagsValidator func(cmd *cobra.Command) error | ||
|
||
func flagValidRepository(flagName string) flagsValidator { | ||
return func(cmd *cobra.Command) error { | ||
repositoryValue := cmd.Flag(flagName).Value.String() | ||
|
||
if strings.HasSuffix(repositoryValue, "/") || strings.Contains(repositoryValue, "//") { | ||
return fmt.Errorf("invalid repository: %s", repositoryValue) | ||
} | ||
|
||
for i, part := range strings.Split(repositoryValue, "/") { | ||
if i != 0 { | ||
if strings.ContainsAny(part, ":@\" ") { | ||
return fmt.Errorf("invalid repository: %s", repositoryValue) | ||
} | ||
continue | ||
} | ||
|
||
authorityParts := strings.Split(part, ":") | ||
if len(authorityParts) > 2 { | ||
return fmt.Errorf("invalid repository hostname: %s", part) | ||
} | ||
if errs := validation.IsDNS1123Subdomain(authorityParts[0]); len(errs) > 0 { | ||
return fmt.Errorf("invalid repository hostname: %s", strings.Join(errs, "; ")) | ||
} | ||
if len(authorityParts) == 2 { | ||
portNumber, err := strconv.Atoi(authorityParts[1]) | ||
if err != nil { | ||
return fmt.Errorf("invalid repository port number: %s", authorityParts[1]) | ||
} | ||
|
||
if errs := validation.IsValidPortNum(portNumber); len(errs) > 0 { | ||
return fmt.Errorf("invalid repository port number: %s", strings.Join(errs, "; ")) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters