ingress-nginx-helm/images/opentelemetry/rootfs/init_module.go

104 lines
2.1 KiB
Go
Raw Normal View History

/*
Copyright 2023 The Kubernetes 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 main
import (
"fmt"
"io"
"os"
"path/filepath"
)
func main() {
// Enable error handling for all operations
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run() error {
// Create the target directory if it doesn't exist
targetDir := "/modules_mount/etc/nginx/modules/otel"
err := os.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
// Copy files from source directory to target directory
sourceDir := "/etc/nginx/modules/"
err = filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
return nil
}
// Calculate the destination path
relPath, err := filepath.Rel(sourceDir, path)
if err != nil {
return err
}
destPath := filepath.Join(targetDir, relPath)
// Create the destination directory if it doesn't exist
destDir := filepath.Dir(destPath)
err = os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return err
}
// Copy the file
err = copyFile(path, destPath)
if err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to copy files: %w", err)
}
return nil
}
func copyFile(sourcePath, destPath string) error {
sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(destPath)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return err
}
return nil
}