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

fix: add files to image issue #596

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 16 additions & 10 deletions e2e/basic/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const (
prometheusConfig = "/etc/prometheus/prometheus.yml"
prometheusArgs = "--config.file=" + prometheusConfig

curlImage = "curlimages/curl:latest"
otlpPort = observability.DefaultOtelOtlpPort
curlImage = "curlimages/curl:latest"
telemetryPort = observability.DefaultTelemetryPort
otlpPort = observability.DefaultOtelOtlpPort

retryInterval = 1 * time.Second
retryTimeout = 10 * time.Second
retryInterval = 3 * time.Second
retryTimeout = 5 * time.Minute
)

// TestObservabilityCollector is a test function that verifies the functionality of the otel collector setup
Expand Down Expand Up @@ -58,7 +59,7 @@ global:
scrape_configs:
- job_name: 'otel-collector'
static_configs:
- targets: ['otel-collector:%d']`, scrapeInterval, otlpPort)
- targets: ['localhost:%d']`, scrapeInterval, telemetryPort)
s.Require().NoError(prometheus.Storage().AddFileBytes([]byte(prometheusConfigContent), prometheusConfig, "0:0"))

s.Require().NoError(prometheus.Build().SetArgs(prometheusArgs))
Expand All @@ -68,9 +69,9 @@ scrape_configs:
observabilitySidecar := observability.New()

s.Require().NoError(observabilitySidecar.SetOtelEndpoint(4318))
s.Require().NoError(observabilitySidecar.SetPrometheusEndpoint(otlpPort, fmt.Sprintf("knuu-%s", s.Knuu.Scope), scrapeInterval))
s.Require().NoError(observabilitySidecar.SetPrometheusEndpoint(telemetryPort, fmt.Sprintf("knuu-%s", s.Knuu.Scope), scrapeInterval))
s.Require().NoError(observabilitySidecar.SetJaegerEndpoint(14250, 6831, 14268))
s.Require().NoError(observabilitySidecar.SetOtlpExporter("prometheus:9090", "", ""))
s.Require().NoError(observabilitySidecar.SetOtlpExporter("http://prometheus:9090", "", ""))

// Create and start a target pod and configure it to use the obsySidecar to push metrics
target, err := s.Knuu.NewInstance(namePrefix + "-target")
Expand Down Expand Up @@ -104,7 +105,11 @@ scrape_configs:
return false
}
if resp.StatusCode != http.StatusOK {
s.T().Logf("Prometheus API returned status code: %d\tRetrying...", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
s.T().Logf("Failed to read response body: %v", err)
}
s.T().Logf("Prometheus API returned status code: %d\tRetrying...\nResponse: %s", resp.StatusCode, string(body))
return false
}

Expand All @@ -117,13 +122,14 @@ scrape_configs:
return strings.Contains(string(body), "otel-collector")

}, retryTimeout, retryInterval, "otel-collector data source not found in Prometheus")

}

func (s *Suite) TestObservabilityCollectorWithLogging() {
const (
namePrefix = "observability"
targetStartCommand = "while true; do curl -X POST http://localhost:8888/v1/traces; sleep 2; done"
namePrefix = "observability"
)
targetStartCommand := fmt.Sprintf("while true; do curl -X POST http://localhost:%d/v1/traces; sleep 2; done", otlpPort)
ctx := context.Background()

// Setup obsySidecar collector
Expand Down
10 changes: 10 additions & 0 deletions e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Suite struct {
cleanupMu sync.Mutex
totalTests atomic.Int32
finishedTests int32

skipCleanup atomic.Bool // just for debugging
}

var (
Expand All @@ -57,12 +59,20 @@ func (s *Suite) TearDownTest() {

func (s *Suite) cleanupSuite() {
s.T().Logf("Cleaning up knuu...")
if s.skipCleanup.Load() {
s.T().Logf("* Cleanup skipped. Note: this is just for debugging purposes. Make sure to remove the SkipCleanup() call in the test before merging.")
return
}
if err := s.Knuu.CleanUp(context.Background()); err != nil {
s.T().Logf("Error cleaning up test suite: %v", err)
}
s.T().Logf("Knuu is cleaned up")
}

func (s *Suite) SkipCleanup() {
s.skipCleanup.Store(true)
}

func (s *Suite) CreateNginxInstance(ctx context.Context, name string) *instance.Instance {
ins, err := s.Knuu.NewInstance(name)
s.Require().NoError(err)
Expand Down
4 changes: 1 addition & 3 deletions e2e/system/build_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (s *Suite) TestBuildFromGit() {
// Setup
ctx := context.Background()

s.T().Log("Creating new instance")
target, err := s.Knuu.NewInstance(namePrefix)
s.Require().NoError(err, "Error creating new instance")

Expand Down Expand Up @@ -61,7 +60,6 @@ func (s *Suite) TestBuildFromGitWithModifications() {
// Setup
ctx := context.Background()

s.T().Log("Creating new instance")
target, err := s.Knuu.NewInstance(namePrefix)
s.Require().NoError(err)

Expand All @@ -79,7 +77,7 @@ func (s *Suite) TestBuildFromGitWithModifications() {
expectedData = "Hello, world!"
)

err = target.Storage().AddFileBytes([]byte(expectedData), filePath, "root:root")
err = target.Storage().AddFileBytes([]byte(expectedData), filePath, "0:0")
s.Require().NoError(err)

s.Require().NoError(target.Build().Commit(ctx))
Expand Down
66 changes: 64 additions & 2 deletions pkg/instance/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import (
"github.com/celestiaorg/knuu/pkg/container"
)

const (
baseInitImageName = "alpine:latest"
)

type build struct {
instance *Instance
imageName string
imagePullPolicy v1.PullPolicy
initImageName string
builderFactory *container.BuilderFactory
imagePullPolicy v1.PullPolicy
command []string
args []string
env map[string]string
Expand Down Expand Up @@ -160,6 +165,56 @@ func (b *build) SetUser(user string) error {
return nil
}

// buildInitImage builds the init image for the instance
// This function can only be called in the state 'Preparing'
func (b *build) buildInitImage(ctx context.Context) error {
if !b.instance.IsState(StatePreparing) {
return ErrBuildingInitImageNotAllowed.WithParams(b.instance.state.String())
}

buildDir, err := b.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}

factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
ImageName: baseInitImageName,
BuildContext: buildDir,
ImageBuilder: b.instance.ImageBuilder,
Logger: b.instance.Logger,
})
if err != nil {
return ErrCreatingBuilder.Wrap(err)
}

mustBuild := false
for _, vol := range b.instance.storage.volumes {
for _, f := range vol.Files() {
// the files are copied to the build dir with the subfolder structure of dest
factory.AddToBuilder(f.Dest, f.Dest, f.Chown)
mustBuild = true
}
}

if !mustBuild {
return nil
}

imageHash, err := factory.GenerateImageHash()
if err != nil {
return ErrGeneratingImageHash.Wrap(err)
}

// TODO: update this when the custom registry PR is merged (#593)
imageName, err := getImageRegistry(imageHash)
if err != nil {
return ErrGettingImageRegistry.Wrap(err)
}

b.initImageName = imageName
return factory.PushBuilderImage(ctx, imageName)
}
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved

func (b *build) SetNodeSelector(nodeSelector map[string]string) error {
if !b.instance.IsInState(StatePreparing, StateCommitted) {
return ErrSettingNodeSelectorNotAllowed.WithParams(b.instance.state.String())
Expand All @@ -176,6 +231,12 @@ func (b *build) Commit(ctx context.Context) error {
return ErrCommittingNotAllowed.WithParams(b.instance.state.String())
}

if len(b.instance.storage.volumes) > 0 {
if err := b.buildInitImage(ctx); err != nil {
return err
}
}

if !b.builderFactory.Changed() {
b.imageName = b.builderFactory.ImageNameFrom()
b.instance.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -254,7 +315,8 @@ func (b *build) getBuildDir() (string, error) {
if err != nil {
return "", err
}
return filepath.Join(tmpDir, b.instance.name), nil
b.buildDir = filepath.Join(tmpDir, b.instance.name)
return b.buildDir, nil
}

// addFileToBuilder adds a file to the builder
Expand Down
2 changes: 2 additions & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,6 @@ var (
ErrFailedToGetFileSize = errors.New("FailedToGetFileSize", "failed to get file size")
ErrFileTooLargeCommitted = errors.New("FileTooLargeCommitted", "file '%s' is too large (max 1MiB) to add after instance is committed")
ErrTotalFilesSizeTooLarge = errors.New("TotalFilesSizeTooLarge", "total files size is too large (max 1MiB)")
ErrFileAlreadyExistsInTheVolumePath = errors.New("FileAlreadyExistsInTheVolumePath", "file %s already exists in the volume path %s")
ErrBuildingInitImageNotAllowed = errors.New("BuildingInitImageNotAllowed", "building init image is only allowed in state 'Preparing'. Current state is '%s'")
)
1 change: 1 addition & 0 deletions pkg/instance/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig {
SecurityContext: e.instance.security.prepareSecurityContext(),
TCPPorts: e.instance.network.portsTCP,
UDPPorts: e.instance.network.portsUDP,
InitImageName: e.instance.build.initImageName,
}

sidecarConfigs := make([]k8s.ContainerConfig, 0)
Expand Down
Loading
Loading