-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/k8scluster] Remove included tags (#6436)
* feat(container.image.name): remove tags included in the name and introduce helper package * fix(container.image.name): use `` instead "" for regex * docs(Changelog): add changes * chore(container.image.name): rename function to ParseImageName * chore(container.image.name): add logging for parse failures * feat(container.image.name): use struct to encapsulate return value * feat(container.image): add sha256 field * chore(container.image.name): implement change requests
- Loading branch information
Showing
14 changed files
with
282 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/docker" | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
extractImageRegexp = regexp.MustCompile(`^(?P<repository>([^/\s]+/)?([^:\s]+))(:(?P<tag>[^@\s]+))?(@sha256:(?P<sha256>\d+))?$`) | ||
) | ||
|
||
type ImageRef struct { | ||
Repository string | ||
Tag string | ||
SHA256 string | ||
} | ||
|
||
// ParseImageName extracts image repository and tag from a combined image reference | ||
// e.g. example.com:5000/alpine/alpine:test --> `example.com:5000/alpine/alpine` and `test` | ||
func ParseImageName(image string) (ImageRef, error) { | ||
if image == "" { | ||
return ImageRef{}, errors.New("empty image") | ||
} | ||
|
||
match := extractImageRegexp.FindStringSubmatch(image) | ||
if len(match) == 0 { | ||
return ImageRef{}, errors.New("failed to match regex against image") | ||
} | ||
|
||
tag := "latest" | ||
if foundTag := match[extractImageRegexp.SubexpIndex("tag")]; foundTag != "" { | ||
tag = foundTag | ||
} | ||
|
||
repository := match[extractImageRegexp.SubexpIndex("repository")] | ||
|
||
hash := match[extractImageRegexp.SubexpIndex("sha256")] | ||
|
||
return ImageRef{ | ||
Repository: repository, | ||
Tag: tag, | ||
SHA256: hash, | ||
}, nil | ||
} | ||
|
||
func LogParseError(err error, image string, logger *zap.Logger) { | ||
logger.Debug(err.Error(), | ||
zap.String("image", image), | ||
) | ||
} |
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,162 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 docker | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDockerImageToElements(t *testing.T) { | ||
type args struct { | ||
image string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantRepository string | ||
wantTag string | ||
wantSHA256 string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty string", | ||
args: args{ | ||
image: "", | ||
}, | ||
wantRepository: "", | ||
wantTag: "", | ||
wantSHA256: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "malformed image", | ||
args: args{ | ||
image: "aaa:", | ||
}, | ||
wantRepository: "", | ||
wantTag: "", | ||
wantSHA256: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "image with sha256 hash", | ||
args: args{ | ||
image: "alpine:test@sha256:00000000000000", | ||
}, | ||
wantRepository: "alpine", | ||
wantTag: "test", | ||
wantSHA256: "00000000000000", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "shorthand only", | ||
args: args{ | ||
image: "alpine", | ||
}, | ||
wantRepository: "alpine", | ||
wantTag: "latest", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "shorthand with tag", | ||
args: args{ | ||
image: "alpine:v1.0.0", | ||
}, | ||
wantRepository: "alpine", | ||
wantTag: "v1.0.0", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository without registry and tag", | ||
args: args{ | ||
image: "alpine/alpine", | ||
}, | ||
wantRepository: "alpine/alpine", | ||
wantTag: "latest", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository without registry", | ||
args: args{ | ||
image: "alpine/alpine:2.0.0", | ||
}, | ||
wantRepository: "alpine/alpine", | ||
wantTag: "2.0.0", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository with registry and without tag", | ||
args: args{ | ||
image: "example.com/alpine/alpine", | ||
}, | ||
wantRepository: "example.com/alpine/alpine", | ||
wantTag: "latest", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository with registry and tag", | ||
args: args{ | ||
image: "example.com/alpine/alpine:1", | ||
}, | ||
wantRepository: "example.com/alpine/alpine", | ||
wantTag: "1", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository with registry and port but without tag", | ||
args: args{ | ||
image: "example.com:3000/alpine/alpine", | ||
}, | ||
wantRepository: "example.com:3000/alpine/alpine", | ||
wantTag: "latest", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "repository with registry, port and tag", | ||
args: args{ | ||
image: "example.com:3000/alpine/alpine:test", | ||
}, | ||
wantRepository: "example.com:3000/alpine/alpine", | ||
wantTag: "test", | ||
wantSHA256: "", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
image, err := ParseImageName(tt.args.image) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ParseImageName() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if image.Repository != tt.wantRepository { | ||
t.Errorf("ParseImageName() repository = %v, want %v", image.Repository, tt.wantRepository) | ||
} | ||
if image.Tag != tt.wantTag { | ||
t.Errorf("ParseImageName() tag = %v, want %v", image.Tag, tt.wantTag) | ||
} | ||
if image.SHA256 != tt.wantSHA256 { | ||
t.Errorf("ParseImageName() hash = %v, want %v", image.SHA256, tt.wantSHA256) | ||
} | ||
}) | ||
} | ||
} |
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.
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
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
Oops, something went wrong.