Skip to content

Commit

Permalink
Merge branch 'main' into fix/endless-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
franknstyle authored Oct 18, 2023
2 parents e9307f0 + dd0e73c commit a008fad
Show file tree
Hide file tree
Showing 77 changed files with 240 additions and 200 deletions.
52 changes: 49 additions & 3 deletions cmd/sonobuoy/app/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
e2eSkipFlag = "e2e-skip"
e2eParallelFlag = "e2e-parallel"
e2eRegistryConfigFlag = "e2e-repo-config"
e2eDockerConfigFileFlag = "e2e-docker-config-file"
e2eRegistryFlag = "e2e-repo"
pluginImageFlag = "plugin-image"
filenameFlag = "filename"
Expand Down Expand Up @@ -224,7 +225,7 @@ func AddSonobuoyConfigFlag(cfg *SonobuoyConfig, flags *pflag.FlagSet) {
// AddLegacyE2EFlags is a way to add flags which target the e2e plugin specifically
// by leveraging the existing flags. They typically wrap other fields (like the env var
// overrides) and modify those.
func AddLegacyE2EFlags(env *PluginEnvVars, pluginTransforms *map[string][]func(*manifest.Manifest) error, fs *pflag.FlagSet) {
func AddLegacyE2EFlags(cfg *SonobuoyConfig, env *PluginEnvVars, pluginTransforms *map[string][]func(*manifest.Manifest) error, fs *pflag.FlagSet) {
m := &Mode{
env: env,
name: "",
Expand Down Expand Up @@ -289,6 +290,15 @@ func AddLegacyE2EFlags(env *PluginEnvVars, pluginTransforms *map[string][]func(*
&envVarModierFlag{plugin: e2ePlugin, field: "KUBE_TEST_REPO", PluginEnvVars: *env}, e2eRegistryFlag,
"Specify a registry to use as the default for pulling Kubernetes test images. Same as providing --e2e-repo-config but specifying the same repo repeatedly.",
)

fs.Var(
&e2eDockerConfigFlag{
plugin: e2ePlugin,
config: cfg,
transforms: *pluginTransforms,
}, e2eDockerConfigFileFlag,
"A docker credentials configuration file used which contains authorization token that can be used to pull images from certain private registries provided by the users",
)
}

// AddRBACModeFlags adds an E2E Argument with the provided default.
Expand Down Expand Up @@ -563,9 +573,11 @@ func (f *e2eRepoFlag) Set(str string) error {
}

f.transforms[f.plugin] = append(f.transforms[f.plugin], func(m *manifest.Manifest) error {
m.ConfigMap = map[string]string{
name: string(fData),
if m.ConfigMap == nil {
m.ConfigMap = map[string]string{}
}
m.ConfigMap[name] = string(fData)

m.Spec.Env = append(m.Spec.Env, corev1.EnvVar{
Name: "KUBE_TEST_REPO_LIST",
Value: fmt.Sprintf("/tmp/sonobuoy/config/%v", name),
Expand All @@ -575,6 +587,40 @@ func (f *e2eRepoFlag) Set(str string) error {
return nil
}

type e2eDockerConfigFlag struct {
plugin string
filename string
config *SonobuoyConfig

transforms map[string][]func(*manifest.Manifest) error
// Value to put in the configmap as the filename. Defaults to filename.
filenameOverride string
}

func (f *e2eDockerConfigFlag) String() string { return f.filename }
func (f *e2eDockerConfigFlag) Type() string { return "json-filepath" }
func (f *e2eDockerConfigFlag) Set(str string) error {
f.config.E2EDockerConfigFile = str
name := filepath.Base(str)
if len(f.filenameOverride) > 0 {
name = f.filenameOverride
}
fData, err := os.ReadFile(str)
if err != nil {
return errors.Wrapf(err, "failed to read file %q", str)
}

f.transforms[e2ePlugin] = append(f.transforms[e2ePlugin], func(m *manifest.Manifest) error {
if m.ConfigMap == nil {
m.ConfigMap = map[string]string{}
}
m.ConfigMap[name] = string(fData)
return nil

})
return nil
}

// The ssh-key flag needs to store the path to the ssh key but also
// wire up the e2e plugin for using it.
type sshPathFlag struct {
Expand Down
36 changes: 35 additions & 1 deletion cmd/sonobuoy/app/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package app
import (
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/vmware-tanzu/sonobuoy/pkg/client"
Expand Down Expand Up @@ -94,7 +96,7 @@ func GenFlagSet(cfg *genFlags, rbac RBACMode) *pflag.FlagSet {

AddPluginSetFlag(&cfg.plugins, genset)
AddPluginEnvFlag(&cfg.pluginEnvs, genset)
AddLegacyE2EFlags(&cfg.pluginEnvs, &cfg.pluginTransforms, genset)
AddLegacyE2EFlags(&cfg.sonobuoyConfig, &cfg.pluginEnvs, &cfg.pluginTransforms, genset)

AddNodeSelectorsFlag(&cfg.nodeSelectors, genset)

Expand Down Expand Up @@ -165,6 +167,15 @@ func (g *genFlags) Config() (*client.GenConfig, error) {
k8sVersion = g.k8sVersion.String()
}

if g.sonobuoyConfig.E2EDockerConfigFile != "" {
if err := verifyKubernetesVersion(k8sVersion); err != nil {
return nil, err
}
if g.sonobuoyConfig.ImagePullSecrets == "" {
g.sonobuoyConfig.ImagePullSecrets = "auth-repo-cred"
}
}

return &client.GenConfig{
Config: &g.sonobuoyConfig.Config,
EnableRBAC: rbacEnabled,
Expand Down Expand Up @@ -207,6 +218,7 @@ func NewCmdGen() *cobra.Command {
Args: cobra.ExactArgs(0),
}
GenCommand.Flags().AddFlagSet(GenFlagSet(&genflags, EnabledRBACMode))

return GenCommand
}

Expand Down Expand Up @@ -261,3 +273,25 @@ func getClient(kubeconfig *Kubeconfig) (*kubernetes.Clientset, error) {

return client, kubeError
}

func verifyKubernetesVersion(k8sVersion string) error {
parts := versionMatchRE.FindStringSubmatch(k8sVersion)
if parts == nil {
return fmt.Errorf("could not parse %q as version", k8sVersion)
}
numbers, _ := parts[1], parts[2]

versionInfo := strings.Split(numbers, ".")
minorVersion := versionInfo[1]

minorVersionInt, err := strconv.ParseInt(minorVersion, 10, 0)
if err != nil {
return err
}

if minorVersionInt < 27 {
err = fmt.Errorf("e2e-docker-config-file is only supported for Kubernetes 1.27 or later")
}

return err
}
53 changes: 53 additions & 0 deletions cmd/sonobuoy/app/gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright the Sonobuoy contributors 2019
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 app

import (
"testing"
)

func TestVerifyKubernetesVersion(t *testing.T) {
testCases := []struct {
desc string
k8sVersion string
expectErr string
}{
{
desc: "Usage of e2e-docker-config-file flag with Kubernetes versions below 1.27 should throw error",
k8sVersion: "1.26-alpha-3",
expectErr: "e2e-docker-config-file is only supported for Kubernetes 1.27 or later",
},
{
desc: "Usage of e2e-docker-config-file flag along with Kubernetes versions 1.27 or later should work as expected",
k8sVersion: "1.27",
},
}

for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
err := verifyKubernetesVersion(testCase.k8sVersion)
if err != nil {
if len(testCase.expectErr) == 0 {
t.Fatalf("Expected nil error but got %v", err)
}
if err.Error() != testCase.expectErr {
t.Fatalf("Expected error %q but got %q", err, testCase.expectErr)
}
}
})

}
}
1 change: 0 additions & 1 deletion cmd/sonobuoy/app/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ func translateRegistry(imageURL string, customRegistry string, customRegistryLis
if len(customRegistry) > 0 {
return fmt.Sprintf("%s/%s", customRegistry, parts[countParts-1])
}

// For now, if not given a customRegistry, assume they gave the customRegistryList as non-nil.
switch registryAndUser {
case "gcr.io/e2e-test-images":
Expand Down
14 changes: 14 additions & 0 deletions cmd/sonobuoy/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"flag"
"fmt"
"regexp"
"sync"

"github.com/sirupsen/logrus"
Expand All @@ -34,6 +35,10 @@ import (
// and cause a panic otherwise.
var once sync.Once

// versionMatchRE splits a version string into numeric and "extra" parts
// source: https://github.com/kubernetes/kubernetes/blob/af1bf4306709020ed2002618e099b3d0acf3c7b5/staging/src/k8s.io/apimachinery/pkg/util/version/version.go#L37
var versionMatchRE = regexp.MustCompile(`^\s*v?([0-9]+(?:\.[0-9]+)*)(.*)*$`)

func NewSonobuoyCommand() *cobra.Command {
cmds := &cobra.Command{
Use: "sonobuoy",
Expand Down Expand Up @@ -106,8 +111,10 @@ func prerunChecks(cmd *cobra.Command, args []string) error {
// Getting a list of all flags provided by the user.
flagsSet := map[string]bool{}
flagsDebug := []string{}
flagArgs := map[string]string{}
cmd.Flags().Visit(func(f *pflag.Flag) {
flagsSet[f.Name] = true
flagArgs[f.Name] = f.Value.String()
flagsDebug = append(flagsDebug, fmt.Sprintf("%v=%v", f.Name, f.Value.String()))
})

Expand Down Expand Up @@ -135,6 +142,13 @@ func prerunChecks(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%v and %v flags are both set and may collide", e2eRegistryConfigFlag, e2eRegistryFlag)
}

if flagsSet[e2eDockerConfigFileFlag] && flagsSet["kubernetes-version"] {
k8sVersion := flagArgs["kubernetes-version"]
if err := verifyKubernetesVersion(k8sVersion); err != nil {
return err
}
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
golang.org/x/sync v0.1.0
golang.org/x/term v0.6.0
golang.org/x/text v0.8.0
golang.org/x/term v0.13.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.27.1
Expand Down Expand Up @@ -65,9 +65,9 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -433,11 +433,11 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -446,8 +446,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
Loading

0 comments on commit a008fad

Please sign in to comment.