Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile: add lint rule for maintainer instruction #4878

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could just have this nil check inside the Run() one time so every caller does not need to check it.

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"
},
}
)
Loading