Skip to content

Commit

Permalink
Merge pull request #4878 from jsternberg/maintainer-lint-rule
Browse files Browse the repository at this point in the history
dockerfile: add lint rule for maintainer instruction
  • Loading branch information
tonistiigi authored Apr 25, 2024
2 parents 79e6e95 + 79f66ec commit 03a2b91
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions frontend/dockerfile/dockerfile_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var lintTests = integration.TestFuncs(
testNoEmptyContinuations,
testSelfConsistentCommandCasing,
testFileConsistentCommandCasing,
testMaintainerDeprecated,
)

func testStageName(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -208,6 +209,29 @@ COPY Dockerfile /bar
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{})
}

func testMaintainerDeprecated(t *testing.T, sb integration.Sandbox) {
dockerfile := []byte(`
FROM scratch
MAINTAINER [email protected]
`)
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{
{
RuleName: "MaintainerDeprecated",
Description: "The maintainer instruction is deprecated, use a label instead to define an image author",
Detail: "Maintainer instruction is deprecated in favor of using label",
URL: "https://docs.docker.com/reference/dockerfile/#maintainer-deprecated",
Level: 1,
Line: 3,
},
})

dockerfile = []byte(`
FROM scratch
LABEL org.opencontainers.image.authors="[email protected]"
`)
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{})
}

func checkUnmarshal(t *testing.T, sb integration.Sandbox, c *client.Client, dir *integration.TmpDirWithName, expected []expectedLintWarning) {
destDir, err := os.MkdirTemp("", "buildkit")
require.NoError(t, err)
Expand Down Expand Up @@ -254,6 +278,8 @@ func checkUnmarshal(t *testing.T, sb integration.Sandbox, c *client.Client, dir
}

func checkProgressStream(t *testing.T, sb integration.Sandbox, c *client.Client, dir *integration.TmpDirWithName, expected []expectedLintWarning) {
t.Helper()

status := make(chan *client.SolveStatus)
statusDone := make(chan struct{})
done := make(chan struct{})
Expand Down Expand Up @@ -335,6 +361,7 @@ func checkLinterWarnings(t *testing.T, sb integration.Sandbox, dockerfile []byte
}

func checkVertexWarning(t *testing.T, warning *client.VertexWarning, expected expectedLintWarning) {
t.Helper()
short := linter.LintFormatShort(expected.RuleName, expected.Detail, expected.Line)
require.Equal(t, short, string(warning.Short))
require.Equal(t, expected.Description, string(warning.Detail[0]))
Expand All @@ -343,6 +370,7 @@ func checkVertexWarning(t *testing.T, warning *client.VertexWarning, expected ex
}

func checkLintWarning(t *testing.T, warning lint.Warning, expected expectedLintWarning) {
t.Helper()
require.Equal(t, expected.RuleName, warning.RuleName)
require.Equal(t, expected.Description, warning.Description)
require.Equal(t, expected.URL, warning.URL)
Expand Down
4 changes: 4 additions & 0 deletions frontend/dockerfile/instructions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func ParseInstructionWithLinter(node *parser.Node, lintWarn linter.LintWarnFunc)
case command.Env:
return parseEnv(req)
case command.Maintainer:
if lintWarn != nil {
msg := linter.RuleMaintainerDeprecated.Format()
linter.RuleMaintainerDeprecated.Run(lintWarn, node.Location(), msg)
}
return parseMaintainer(req)
case command.Label:
return parseLabel(req)
Expand Down
8 changes: 8 additions & 0 deletions frontend/dockerfile/linter/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ var (
return fmt.Sprintf("Command '%s' should match the case of the command majority (%s)", violatingCommand, correctCasing)
},
}
RuleMaintainerDeprecated = LinterRule[func() string]{
Name: "MaintainerDeprecated",
Description: "The maintainer instruction is deprecated, use a label instead to define an image author",
URL: "https://docs.docker.com/reference/dockerfile/#maintainer-deprecated",
Format: func() string {
return "Maintainer instruction is deprecated in favor of using label"
},
}
)

0 comments on commit 03a2b91

Please sign in to comment.