-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a general container lister and implement sample command to test it out Signed-off-by: Juan Antonio Osorio <ozz@stacklok.com>
- Loading branch information
Showing
45 changed files
with
3,059 additions
and
1,967 deletions.
There are no files selected for viewing
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
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
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,102 @@ | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// 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 image | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/stacklok/minder/internal/providers/credentials" | ||
"github.com/stacklok/minder/internal/providers/dockerhub" | ||
"github.com/stacklok/minder/internal/providers/github/ghcr" | ||
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" | ||
provifv1 "github.com/stacklok/minder/pkg/providers/v1" | ||
) | ||
|
||
// CmdList returns the command for listing containers | ||
func CmdList() *cobra.Command { | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "list images", | ||
RunE: runCmdList, | ||
SilenceUsage: true, | ||
} | ||
|
||
listCmd.Flags().StringP("provider", "p", "", "provider class to use for listing containers") | ||
listCmd.Flags().StringP("namespace", "n", "", "namespace to list containers from") | ||
//nolint:goconst // let's not use a const for this one | ||
listCmd.Flags().StringP("token", "t", "", "token to authenticate to the provider."+ | ||
//nolint:goconst // let's not use a const for this one | ||
"Can also be set via the AUTH_TOKEN environment variable.") | ||
|
||
if err := viper.BindPFlag("auth.token", listCmd.Flags().Lookup("token")); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error binding flag: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
|
||
return listCmd | ||
} | ||
|
||
func runCmdList(cmd *cobra.Command, _ []string) error { | ||
ctx := context.Background() | ||
|
||
// get the provider | ||
pclass := cmd.Flag("provider") | ||
if pclass.Value.String() == "" { | ||
return fmt.Errorf("provider class is required") | ||
} | ||
ns := cmd.Flag("namespace") | ||
if ns.Value.String() == "" { | ||
return fmt.Errorf("namespace is required") | ||
} | ||
|
||
var prov provifv1.ImageLister | ||
switch pclass.Value.String() { | ||
case "dockerhub": | ||
var err error | ||
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token")) | ||
prov, err = dockerhub.New(cred, &minderv1.DockerHubProviderConfig{ | ||
Namespace: ns.Value.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
case "ghcr": | ||
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token")) | ||
prov = ghcr.New(cred, ns.Value.String()) | ||
default: | ||
return fmt.Errorf("unknown provider: %s", pclass.Value.String()) | ||
} | ||
|
||
// get the containers | ||
containers, err := prov.ListImages(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// print the containers | ||
for _, container := range containers { | ||
fmt.Println(container) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// 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 image | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/stacklok/minder/internal/providers/credentials" | ||
"github.com/stacklok/minder/internal/providers/oci" | ||
) | ||
|
||
// CmdListTags returns the command for listing container tags | ||
func CmdListTags() *cobra.Command { | ||
var listCmd = &cobra.Command{ | ||
Use: "list-tags", | ||
Short: "list container tags", | ||
RunE: runCmdListTags, | ||
SilenceUsage: true, | ||
} | ||
|
||
listCmd.Flags().StringP("base-url", "b", "", "base URL for the OCI registry") | ||
listCmd.Flags().StringP("container", "c", "", "container name to list tags for") | ||
//nolint:goconst // let's not use a const for this one | ||
listCmd.Flags().StringP("token", "t", "", "token to authenticate to the provider."+ | ||
"Can also be set via the AUTH_TOKEN environment variable.") | ||
|
||
if err := viper.BindPFlag("auth.token", listCmd.Flags().Lookup("token")); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error binding flag: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
|
||
return listCmd | ||
} | ||
|
||
func runCmdListTags(cmd *cobra.Command, _ []string) error { | ||
ctx := context.Background() | ||
|
||
// get the provider | ||
baseURL := cmd.Flag("base-url") | ||
contname := cmd.Flag("container") | ||
|
||
if baseURL.Value.String() == "" { | ||
return fmt.Errorf("base URL is required") | ||
} | ||
if contname.Value.String() == "" { | ||
return fmt.Errorf("container name is required") | ||
} | ||
|
||
cred := credentials.NewOAuth2TokenCredential(viper.GetString("auth.token")) | ||
prov := oci.New(cred, baseURL.Value.String()) | ||
|
||
// get the containers | ||
containers, err := prov.ListTags(ctx, contname.Value.String()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// print the containers | ||
for _, container := range containers { | ||
fmt.Println(container) | ||
} | ||
|
||
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
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,13 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- 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. |
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,19 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- 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. | ||
|
||
ALTER TYPE provider_type ADD VALUE 'image-lister'; | ||
|
||
-- Add `ghcr` and `dockerhub` provider classes | ||
ALTER TYPE provider_class ADD VALUE 'ghcr'; | ||
ALTER TYPE provider_class ADD VALUE 'dockerhub'; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 Stacklok, Inc | ||
// | ||
// 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 constants | ||
|
||
var ( | ||
// ServerUserAgent is the user agent string for the server | ||
ServerUserAgent = "Minder/" + Revision | ||
) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.