Skip to content

Commit fa2491d

Browse files
committed
Merge branch 'master' of https://github.com/GoogleContainerTools/skaffold into task-event
2 parents e4d23cd + f280aa8 commit fa2491d

File tree

23 files changed

+258
-154
lines changed

23 files changed

+258
-154
lines changed

cmd/skaffold/app/cmd/flags.go

+54-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"reflect"
2222
"time"
2323

24+
"github.com/sirupsen/logrus"
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/pflag"
2627

@@ -517,8 +518,16 @@ func (fl *Flag) flag(cmdName string) *pflag.Flag {
517518
if methodName == "" {
518519
methodName = methodNameByType(reflect.ValueOf(fl.Value))
519520
}
521+
isVar := methodName == "Var"
522+
// pflags' Var*() methods do not take a default value but instead
523+
// assume the value is already set to its default value. So we
524+
// explicitly set the default value here to ensure help text is correct.
525+
if isVar {
526+
setDefaultValues(fl.Value, fl, cmdName)
527+
}
528+
520529
inputs := []interface{}{fl.Value, fl.Name}
521-
if methodName != "Var" {
530+
if !isVar {
522531
if d, found := fl.DefValuePerCommand[cmdName]; found {
523532
inputs = append(inputs, d)
524533
} else {
@@ -528,8 +537,8 @@ func (fl *Flag) flag(cmdName string) *pflag.Flag {
528537
inputs = append(inputs, fl.Usage)
529538

530539
fs := pflag.NewFlagSet(fl.Name, pflag.ContinueOnError)
531-
532540
reflect.ValueOf(fs).MethodByName(methodName).Call(reflectValueOf(inputs))
541+
533542
f := fs.Lookup(fl.Name)
534543
if len(fl.NoOptDefVal) > 0 {
535544
// f.NoOptDefVal may be set depending on value type
@@ -540,37 +549,35 @@ func (fl *Flag) flag(cmdName string) *pflag.Flag {
540549
return f
541550
}
542551

543-
func reflectValueOf(values []interface{}) []reflect.Value {
544-
var results []reflect.Value
545-
for _, v := range values {
546-
results = append(results, reflect.ValueOf(v))
547-
}
548-
return results
549-
}
550-
551552
func ResetFlagDefaults(cmd *cobra.Command, flags []*Flag) {
552553
// Update default values.
553554
for _, fl := range flags {
554555
flag := cmd.Flag(fl.Name)
555556
if !flag.Changed {
556-
defValue := fl.DefValue
557-
if fl.DefValuePerCommand != nil {
558-
if d, present := fl.DefValuePerCommand[cmd.Use]; present {
559-
defValue = d
560-
}
561-
}
562-
if sv, ok := flag.Value.(pflag.SliceValue); ok {
563-
reflect.ValueOf(sv).MethodByName("Replace").Call(reflectValueOf([]interface{}{defValue}))
564-
} else {
565-
flag.Value.Set(fmt.Sprintf("%v", defValue))
566-
}
557+
setDefaultValues(flag.Value, fl, cmd.Name())
567558
}
568559
if fl.IsEnum {
569560
instrumentation.AddFlag(flag)
570561
}
571562
}
572563
}
573564

565+
// setDefaultValues sets the default value (or values) for the given flag definition.
566+
// This function handles pflag's SliceValue and Value interfaces.
567+
func setDefaultValues(v interface{}, fl *Flag, cmdName string) {
568+
d, found := fl.DefValuePerCommand[cmdName]
569+
if !found {
570+
d = fl.DefValue
571+
}
572+
if sv, ok := v.(pflag.SliceValue); ok {
573+
sv.Replace(asStringSlice(d))
574+
} else if val, ok := v.(pflag.Value); ok {
575+
val.Set(fmt.Sprintf("%v", d))
576+
} else {
577+
logrus.Fatalf("%s --%s: unhandled value type: %v (%T)", cmdName, fl.Name, v, v)
578+
}
579+
}
580+
574581
// AddFlags adds to the command the common flags that are annotated with the command name.
575582
func AddFlags(cmd *cobra.Command) {
576583
var flagsForCommand []*Flag
@@ -613,3 +620,29 @@ func hasCmdAnnotation(cmdName string, annotations []string) bool {
613620
}
614621
return false
615622
}
623+
624+
func reflectValueOf(values []interface{}) []reflect.Value {
625+
var results []reflect.Value
626+
for _, v := range values {
627+
results = append(results, reflect.ValueOf(v))
628+
}
629+
return results
630+
}
631+
632+
func asStringSlice(v interface{}) []string {
633+
vt := reflect.TypeOf(v)
634+
if vt == reflect.TypeOf([]string{}) {
635+
return v.([]string)
636+
}
637+
switch vt.Kind() {
638+
case reflect.Array, reflect.Slice:
639+
value := reflect.ValueOf(v)
640+
var slice []string
641+
for i := 0; i < value.Len(); i++ {
642+
slice = append(slice, fmt.Sprintf("%v", value.Index(i)))
643+
}
644+
return slice
645+
default:
646+
return []string{fmt.Sprintf("%v", v)}
647+
}
648+
}

cmd/skaffold/app/cmd/flags_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/spf13/cobra"
@@ -174,3 +175,22 @@ func TestResetFlagDefaults(t *testing.T) {
174175
})
175176
}
176177
}
178+
179+
func TestAsStringSlice(t *testing.T) {
180+
tests := []struct {
181+
input interface{}
182+
expected []string
183+
}{
184+
{"string", []string{"string"}},
185+
{0, []string{"0"}},
186+
{[]string{"a", "b"}, []string{"a", "b"}},
187+
{[]int{0, 1}, []string{"0", "1"}},
188+
}
189+
for _, test := range tests {
190+
testutil.Run(t, fmt.Sprintf("%v", test.expected), func(t *testutil.T) {
191+
result := asStringSlice(test.input)
192+
193+
t.CheckDeepEqual(test.expected, result)
194+
})
195+
}
196+
}

integration/examples/jib-gradle/build.gradle

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ plugins {
33
id 'groovy'
44
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
55
id 'net.ltgt.apt-idea' version '0.18'
6-
id 'com.google.cloud.tools.jib' version '2.8.0'
6+
id 'com.google.cloud.tools.jib' version '3.0.0'
77
}
88

9+
sourceCompatibility = 1.8
10+
targetCompatibility = 1.8
11+
912
version '0.1'
1013
group 'example.jib-gradle'
1114

@@ -35,3 +38,5 @@ dependencies {
3538
compileJava.options.compilerArgs += '-parameters'
3639
compileTestJava.options.compilerArgs += '-parameters'
3740
mainClassName = 'example.micronaut.Application'
41+
42+
jib.from.image = 'gcr.io/google-appengine/openjdk:8'

integration/examples/jib-multimodule/pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<properties>
2020
<java.version>1.8</java.version>
21-
<jib.maven-plugin-version>2.8.0</jib.maven-plugin-version>
21+
<jib.maven-plugin-version>3.0.0</jib.maven-plugin-version>
2222
</properties>
2323

2424
<dependencies>
@@ -40,6 +40,11 @@
4040
<groupId>com.google.cloud.tools</groupId>
4141
<artifactId>jib-maven-plugin</artifactId>
4242
<version>${jib.maven-plugin-version}</version>
43+
<configuration>
44+
<from>
45+
<image>gcr.io/google-appengine/openjdk:8</image>
46+
</from>
47+
</configuration>
4348
</plugin>
4449
</plugins>
4550
</pluginManagement>

integration/examples/jib-sync/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java'
33
id 'org.springframework.boot' version '2.0.5.RELEASE'
44
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
5-
id 'com.google.cloud.tools.jib' version '2.8.0'
5+
id 'com.google.cloud.tools.jib' version '3.0.0'
66
}
77

88
repositories {
@@ -21,3 +21,5 @@ dependencies {
2121

2222
testImplementation "org.springframework.boot:spring-boot-starter-test"
2323
}
24+
25+
jib.from.image = 'gcr.io/google-appengine/openjdk:8'

integration/examples/jib-sync/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<properties>
1111
<java.version>1.8</java.version>
12-
<jib.maven-plugin-version>2.8.0</jib.maven-plugin-version>
12+
<jib.maven-plugin-version>3.0.0</jib.maven-plugin-version>
1313
</properties>
1414

1515
<parent>
@@ -37,6 +37,9 @@
3737
<artifactId>jib-maven-plugin</artifactId>
3838
<version>${jib.maven-plugin-version}</version>
3939
<configuration>
40+
<from>
41+
<image>gcr.io/google-appengine/openjdk:8</image>
42+
</from>
4043
<container>
4144
<jvmFlags>
4245
<jvmFlag>-Djava.security.egd=file:/dev/./urandom</jvmFlag>

integration/examples/jib/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<properties>
1111
<java.version>1.8</java.version>
12-
<jib.maven-plugin-version>2.8.0</jib.maven-plugin-version>
12+
<jib.maven-plugin-version>3.0.0</jib.maven-plugin-version>
1313
</properties>
1414

1515
<parent>
@@ -37,6 +37,9 @@
3737
<artifactId>jib-maven-plugin</artifactId>
3838
<version>${jib.maven-plugin-version}</version>
3939
<configuration>
40+
<from>
41+
<image>gcr.io/google-appengine/openjdk:8</image>
42+
</from>
4043
<container>
4144
<jvmFlags>
4245
<jvmFlag>-Djava.security.egd=file:/dev/./urandom</jvmFlag>

integration/testdata/debug/java/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<description>Simple Java server with Skaffold and Jib</description>
99

1010
<properties>
11-
<jib.maven-plugin-version>2.8.0</jib.maven-plugin-version>
11+
<jib.maven-plugin-version>3.0.0</jib.maven-plugin-version>
1212
<maven.compiler.source>1.8</maven.compiler.source>
1313
<maven.compiler.target>1.8</maven.compiler.target>
1414
</properties>

integration/testdata/jib/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<description>Simple Java server with Skaffold and Jib</description>
99

1010
<properties>
11-
<jib.maven-plugin-version>2.8.0</jib.maven-plugin-version>
11+
<jib.maven-plugin-version>3.0.0</jib.maven-plugin-version>
1212
<maven.compiler.source>1.8</maven.compiler.source>
1313
<maven.compiler.target>1.8</maven.compiler.target>
1414
</properties>
@@ -21,6 +21,9 @@
2121
<artifactId>jib-maven-plugin</artifactId>
2222
<version>${jib.maven-plugin-version}</version>
2323
<configuration>
24+
<from>
25+
<image>gcr.io/google-appengine/openjdk:8</image>
26+
</from>
2427
<container>
2528
<jvmFlags>
2629
<jvmFlag>-Djava.security.egd=file:/dev/./urandom</jvmFlag>

pkg/skaffold/constants/constants.go

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ import (
2323
)
2424

2525
const (
26+
// These are phases in Skaffold
27+
DevLoop = Phase("DevLoop")
28+
Init = Phase("Init")
29+
Build = Phase("Build")
30+
Test = Phase("Test")
31+
Deploy = Phase("Deploy")
32+
StatusCheck = Phase("StatusCheck")
33+
Sync = Phase("Sync")
34+
DevInit = Phase("DevInit")
35+
Cleanup = Phase("Cleanup")
36+
2637
// DefaultLogLevel is the default global verbosity
2738
DefaultLogLevel = logrus.WarnLevel
2839

@@ -60,6 +71,8 @@ const (
6071
GithubIssueLink = "https://github.com/GoogleContainerTools/skaffold/issues/new"
6172
)
6273

74+
type Phase string
75+
6376
var (
6477
Pod latest.ResourceType = "pod"
6578
Service latest.ResourceType = "service"

pkg/skaffold/deploy/kpt/kpt.go

+26
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ func (k *Deployer) renderManifests(ctx context.Context, _ io.Writer, builds []gr
286286
}
287287

288288
// Hydrate the manifests source.
289+
// Note: kustomize cannot be used as a kpt fn yet and thus we add a kustomize cmd step in the kpt pipeline:
290+
// kpt source --> (workaround) kustomize build --> kpt run --> kpt sink.
289291
_, err = kustomize.FindKustomizationConfig(k.Dir)
290292
// Only run kustomize if kustomization.yaml is found.
291293
if err == nil {
@@ -312,7 +314,31 @@ func (k *Deployer) renderManifests(ctx context.Context, _ io.Writer, builds []gr
312314
if err != nil {
313315
return nil, fmt.Errorf("kustomize build: %w", err)
314316
}
317+
} else {
318+
// Note: Here we use kpt ResourceList as the media to store the config source.
319+
// Eventually, skaffold should not need to construct a kpt inner resource but only use kpt commands and
320+
// the new Kptfile(v1) to establish a hydration pipeline.
321+
input := bytes.NewBufferString(string(buf))
322+
rl := framework.ResourceList{
323+
Reader: input,
324+
}
325+
// Manipulate the kustomize "Rnode"(Kustomize term) and pulls out the "Items"
326+
// from ResourceLists.
327+
if err := rl.Read(); err != nil {
328+
return nil, fmt.Errorf("reading ResourceList %w", err)
329+
}
330+
331+
var newBuf []byte
332+
for i := range rl.Items {
333+
item, err := rl.Items[i].String()
334+
if err != nil {
335+
return nil, fmt.Errorf("reading Item %w", err)
336+
}
337+
newBuf = append(newBuf, []byte(item)...)
338+
}
339+
buf = newBuf
315340
}
341+
316342
// Run kpt functions against the hydrated manifests.
317343
cmd = exec.CommandContext(ctx, "kpt", kptCommandArgs("", []string{"fn", "run"}, flags, nil)...)
318344
buf = append(buf, []byte("---\n")...)

0 commit comments

Comments
 (0)