resources

package
v0.0.0-...-f9fc838 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SpringBootConventions = []Convention{
	&BasicConvention{
		Id: "spring-boot",
		Applicable: func(ctx context.Context, metadata ImageMetadata) bool {
			deps := GetDependenciesBOM(ctx)
			return deps.HasDependency("spring-boot")
		},
		Apply: func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error {
			deps := GetDependenciesBOM(ctx)
			setLabel(target, "conventions.carto.run/framework", "spring-boot")
			if springBootDependency := deps.Dependency("spring-boot"); springBootDependency != nil {
				setAnnotation(target, "boot.spring.io/version", springBootDependency.Version)
			}
			return nil
		},
	},
	&BasicConvention{
		Id: "spring-boot-graceful-shutdown",
		Applicable: func(ctx context.Context, metadata ImageMetadata) bool {
			deps := GetDependenciesBOM(ctx)
			return deps.HasDependencyConstraint("spring-boot", ">= 2.3.0-0") && deps.HasDependency(
				"spring-boot-starter-tomcat",
				"spring-boot-starter-jetty",
				"spring-boot-starter-reactor-netty",
				"spring-boot-starter-undertow",
			)
		},
		Apply: func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error {
			applicationProperties := GetSpringApplicationProperties(ctx)

			var k8sGracePeriodSeconds int64 = 30 // default k8s grace period is 30 seconds
			if target.Spec.TerminationGracePeriodSeconds != nil {
				k8sGracePeriodSeconds = *target.Spec.TerminationGracePeriodSeconds
			}
			target.Spec.TerminationGracePeriodSeconds = &k8sGracePeriodSeconds

			bootGracePeriodSeconds := int(math.Floor(0.8 * float64(k8sGracePeriodSeconds)))
			applicationProperties["server.shutdown.grace-period"] = fmt.Sprintf("%ds", bootGracePeriodSeconds)
			return nil
		},
	},
	&BasicConvention{
		Id: "spring-boot-web",
		Applicable: func(ctx context.Context, metadata ImageMetadata) bool {
			deps := GetDependenciesBOM(ctx)
			return deps.HasDependency("spring-boot") && deps.HasDependency("spring-web")
		},
		Apply: func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error {
			applicationProperties := GetSpringApplicationProperties(ctx)

			serverPort := applicationProperties.Default("server.port", "8080")
			port, err := strconv.Atoi(serverPort)
			if err != nil {
				return err
			}

			c := &target.Spec.Containers[containerIdx]

			if name, cp := findContainerPort(target.Spec, int32(port)); cp == nil {
				c.Ports = append(c.Ports, corev1.ContainerPort{
					ContainerPort: int32(port),
					Protocol:      corev1.ProtocolTCP,
				})
			} else if name != c.Name {

				return fmt.Errorf("desired port %s is in use by container %q, set 'server.port' boot property to an open port", serverPort, name)
			}

			return nil
		},
	},
	&BasicConvention{
		Id: "spring-boot-actuator",
		Applicable: func(ctx context.Context, metadata ImageMetadata) bool {
			deps := GetDependenciesBOM(ctx)
			return deps.HasDependency("spring-boot-actuator")
		},
		Apply: func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error {
			applicationProperties := GetSpringApplicationProperties(ctx)

			managementPort := applicationProperties.Default("management.server.port", applicationProperties["server.port"])
			managementBasePath := applicationProperties.Default("management.endpoints.web.base-path", "/actuator")
			managementScheme := corev1.URISchemeHTTP
			if applicationProperties["management.server.ssl.enabled"] == "true" {
				managementScheme = corev1.URISchemeHTTPS
			}

			managementUri := fmt.Sprintf("%s://:%s%s", strings.ToLower(string(managementScheme)), managementPort, managementBasePath)
			setAnnotation(target, "boot.spring.io/actuator", managementUri)

			return nil
		},
	},
	&BasicConvention{
		Id: "spring-boot-actuator-probes",
		Applicable: func(ctx context.Context, metadata ImageMetadata) bool {
			deps := GetDependenciesBOM(ctx)
			return deps.HasDependency("spring-boot-actuator")
		},
		Apply: func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error {
			deps := GetDependenciesBOM(ctx)
			applicationProperties := GetSpringApplicationProperties(ctx)

			if v := applicationProperties.Default("management.health.probes.enabled", "true"); v != "true" {

				return nil
			}

			managementBasePath := applicationProperties["management.endpoints.web.base-path"]
			managementPort, err := strconv.Atoi(applicationProperties["management.server.port"])
			if err != nil {
				return err
			}
			managementScheme := corev1.URISchemeHTTP
			if applicationProperties["management.server.ssl.enabled"] == "true" {
				managementScheme = corev1.URISchemeHTTPS
			}

			var livenessEndpoint, readinessEndpoint string
			if deps.HasDependencyConstraint("spring-boot-actuator", ">= 2.3.0-0") {
				livenessEndpoint = "/health/liveness"
				readinessEndpoint = "/health/readiness"
			} else {
				livenessEndpoint = "/info"
				readinessEndpoint = "/info"
			}

			c := &target.Spec.Containers[containerIdx]

			if c.LivenessProbe == nil {
				c.LivenessProbe = &corev1.Probe{}
			}
			if c.LivenessProbe.ProbeHandler == (corev1.ProbeHandler{}) {
				c.LivenessProbe.ProbeHandler = corev1.ProbeHandler{
					HTTPGet: &corev1.HTTPGetAction{
						Path:   managementBasePath + livenessEndpoint,
						Port:   intstr.FromInt(managementPort),
						Scheme: managementScheme,
					},
				}
			}
			if c.ReadinessProbe == nil {
				c.ReadinessProbe = &corev1.Probe{}
			}
			if c.ReadinessProbe.ProbeHandler == (corev1.ProbeHandler{}) {
				c.ReadinessProbe.ProbeHandler = corev1.ProbeHandler{
					HTTPGet: &corev1.HTTPGetAction{
						Path:   managementBasePath + readinessEndpoint,
						Port:   intstr.FromInt(managementPort),
						Scheme: managementScheme,
					},
				}
			}
			if c.StartupProbe == nil {

				c.StartupProbe = &corev1.Probe{
					InitialDelaySeconds: 1,
					PeriodSeconds:       1,
					FailureThreshold:    120,
				}
			}
			if c.StartupProbe.ProbeHandler == (corev1.ProbeHandler{}) {
				c.StartupProbe.ProbeHandler = *c.LivenessProbe.ProbeHandler.DeepCopy()
			}

			return nil
		},
	},

	&SpringBootServiceIntent{
		Id:        "service-intent-mysql",
		LabelName: "services.conventions.carto.run/mysql",
		Dependencies: []string{
			"mysql-connector-java",
			"r2dbc-mysql",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-postgres",
		LabelName: "services.conventions.carto.run/postgres",
		Dependencies: []string{
			"postgresql",
			"r2dbc-postgresql",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-mongodb",
		LabelName: "services.conventions.carto.run/mongodb",
		Dependencies: []string{
			"mongodb-driver-core",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-rabbitmq",
		LabelName: "services.conventions.carto.run/rabbitmq",
		Dependencies: []string{
			"amqp-client",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-redis",
		LabelName: "services.conventions.carto.run/redis",
		Dependencies: []string{
			"jedis",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-kafka",
		LabelName: "services.conventions.carto.run/kafka",
		Dependencies: []string{
			"kafka-clients",
		},
	},
	&SpringBootServiceIntent{
		Id:        "service-intent-kafka-streams",
		LabelName: "services.conventions.carto.run/kafka-streams",
		Dependencies: []string{
			"kafka-streams",
		},
	},
}

Functions

func StashDependenciesBOM

func StashDependenciesBOM(ctx context.Context, props *DependenciesBOM) context.Context

func StashSpringApplicationProperties

func StashSpringApplicationProperties(ctx context.Context, props SpringApplicationProperties) context.Context

Types

type BasicConvention

type BasicConvention struct {
	Id         string
	Applicable func(ctx context.Context, metadata ImageMetadata) bool
	Apply      func(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error
}

func (*BasicConvention) ApplyConvention

func (o *BasicConvention) ApplyConvention(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error

func (*BasicConvention) GetId

func (o *BasicConvention) GetId() string

func (*BasicConvention) IsApplicable

func (o *BasicConvention) IsApplicable(ctx context.Context, metadata ImageMetadata) bool

type Convention

type Convention interface {
	GetId() string
	IsApplicable(ctx context.Context, metadata ImageMetadata) bool
	ApplyConvention(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error
}

type DependenciesBOM

type DependenciesBOM struct {
	// contains filtered or unexported fields
}

func GetDependenciesBOM

func GetDependenciesBOM(ctx context.Context) *DependenciesBOM

func NewDependenciesBOM

func NewDependenciesBOM(boms []webhookv1alpha1.BOM) DependenciesBOM

func (*DependenciesBOM) Dependency

func (m *DependenciesBOM) Dependency(name string) *cyclonedx.Component

func (*DependenciesBOM) HasDependency

func (m *DependenciesBOM) HasDependency(names ...string) bool

func (*DependenciesBOM) HasDependencyConstraint

func (m *DependenciesBOM) HasDependencyConstraint(name, constraint string) bool

type ImageMetadata

type ImageMetadata = map[string]webhookv1alpha1.ImageConfig

type SpringApplicationProperties

type SpringApplicationProperties map[string]string

func GetSpringApplicationProperties

func GetSpringApplicationProperties(ctx context.Context) SpringApplicationProperties

func (SpringApplicationProperties) Default

func (p SpringApplicationProperties) Default(key string, defaultValue string) string

func (SpringApplicationProperties) FromContainer

func (p SpringApplicationProperties) FromContainer(c *corev1.Container)

func (SpringApplicationProperties) ToContainer

func (p SpringApplicationProperties) ToContainer(c *corev1.Container)

type SpringBootServiceIntent

type SpringBootServiceIntent struct {
	Id           string
	LabelName    string
	Dependencies []string
}

func (*SpringBootServiceIntent) ApplyConvention

func (o *SpringBootServiceIntent) ApplyConvention(ctx context.Context, target *corev1.PodTemplateSpec, containerIdx int, metadata ImageMetadata) error

func (*SpringBootServiceIntent) GetId

func (o *SpringBootServiceIntent) GetId() string

func (*SpringBootServiceIntent) IsApplicable

func (o *SpringBootServiceIntent) IsApplicable(ctx context.Context, metadata ImageMetadata) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL