/
containers.go
187 lines (160 loc) · 5.95 KB
/
containers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2022-2024 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package provider
import (
"bytes"
"context"
"errors"
"fmt"
defaultLog "log"
"strings"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/test-network-function/cnf-certification-test/internal/log"
corev1 "k8s.io/api/core/v1"
"github.com/redhat-openshift-ecosystem/openshift-preflight/artifacts"
plibRuntime "github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
plibContainer "github.com/redhat-openshift-ecosystem/openshift-preflight/container"
)
var (
// Certain tests that have been known to fail because of injected containers (such as Istio) that fail certain tests.
ignoredContainerNames = []string{"istio-proxy"}
)
// Tag and Digest should not be populated at the same time. Digest takes precedence if both are populated
type ContainerImageIdentifier struct {
// Repository is the name of the image that you want to check if exists in the RedHat catalog
Repository string `yaml:"repository" json:"repository"`
// Registry is the name of the registry `docker.io` of the container
// This is valid for container only and required field
Registry string `yaml:"registry" json:"registry"`
// Tag is the optional image tag. "latest" is implied if not specified
Tag string `yaml:"tag" json:"tag"`
// Digest is the image digest following the "@" in a URL, e.g. image@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
Digest string `yaml:"digest" json:"digest"`
}
type Container struct {
*corev1.Container
Status corev1.ContainerStatus
Namespace string
Podname string
NodeName string
Runtime string
UID string
ContainerImageIdentifier ContainerImageIdentifier
PreflightResults PreflightResultsDB
}
func NewContainer() *Container {
return &Container{
Container: &corev1.Container{}, // initialize the corev1.Container object
}
}
func (c *Container) GetUID() (string, error) {
split := strings.Split(c.Status.ContainerID, "://")
uid := ""
if len(split) > 0 {
uid = split[len(split)-1]
}
if uid == "" {
log.Debug(fmt.Sprintf("could not find uid of %s/%s/%s\n", c.Namespace, c.Podname, c.Name))
return "", errors.New("cannot determine container UID")
}
log.Debug(fmt.Sprintf("uid of %s/%s/%s=%s\n", c.Namespace, c.Podname, c.Name, uid))
return uid, nil
}
func (c *Container) SetPreflightResults(preflightImageCache map[string]PreflightResultsDB, env *TestEnvironment) error {
log.Info("Running Preflight container test for container %q with image %q", c, c.Image)
// Short circuit if the image already exists in the cache
if _, exists := preflightImageCache[c.Image]; exists {
log.Info("Container image %q exists in the cache. Skipping this run.", c.Image)
c.PreflightResults = preflightImageCache[c.Image]
return nil
}
opts := []plibContainer.Option{}
opts = append(opts, plibContainer.WithDockerConfigJSONFromFile(env.GetDockerConfigFile()))
if env.IsPreflightInsecureAllowed() {
log.Info("Insecure connections are being allowed to Preflight")
opts = append(opts, plibContainer.WithInsecureConnection())
}
// Create artifacts handler
artifactsWriter, err := artifacts.NewMapWriter()
if err != nil {
return err
}
ctx := artifacts.ContextWithWriter(context.TODO(), artifactsWriter)
// Add logger output to the context
logbytes := bytes.NewBuffer([]byte{})
checklogger := defaultLog.Default()
checklogger.SetOutput(logbytes)
logger := stdr.New(checklogger)
ctx = logr.NewContext(ctx, logger)
check := plibContainer.NewCheck(c.Image, opts...)
results, runtimeErr := check.Run(ctx)
if runtimeErr != nil {
_, checks, err := check.List(ctx)
if err != nil {
return fmt.Errorf("could not get preflight container test list")
}
results.TestedImage = c.Image
for _, c := range checks {
results.PassedOverall = false
result := plibRuntime.Result{Check: c, ElapsedTime: 0}
results.Errors = append(results.Errors, *result.WithError(runtimeErr))
}
}
// Take all of the Preflight logs and stick them into our log.
log.Info(logbytes.String())
// Store the Preflight test results into the container's PreflightResults var and into the cache.
resultsDB := GetPreflightResultsDB(&results)
c.PreflightResults = resultsDB
preflightImageCache[c.Image] = resultsDB
return nil
}
func (c *Container) StringLong() string {
return fmt.Sprintf("node: %s ns: %s podName: %s containerName: %s containerUID: %s containerRuntime: %s",
c.NodeName,
c.Namespace,
c.Podname,
c.Name,
c.Status.ContainerID,
c.Runtime,
)
}
func (c *Container) String() string {
return fmt.Sprintf("container: %s pod: %s ns: %s",
c.Name,
c.Podname,
c.Namespace,
)
}
func (c *Container) HasIgnoredContainerName() bool {
for _, ign := range ignoredContainerNames {
if c.IsIstioProxy() || strings.Contains(c.Name, ign) {
return true
}
}
return false
}
func (c *Container) IsIstioProxy() bool {
return c.Name == "istio-proxy" //nolint:goconst
}
func (c *Container) HasExecProbes() bool {
return c.LivenessProbe != nil && c.LivenessProbe.Exec != nil ||
c.ReadinessProbe != nil && c.ReadinessProbe.Exec != nil ||
c.StartupProbe != nil && c.StartupProbe.Exec != nil
}
func (c *Container) IsTagEmpty() bool {
return c.ContainerImageIdentifier.Tag == ""
}