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

Correctly map metadata #1200

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,19 @@ private static ManifestV3Application.Builder toApplicationManifest(Map<String, O
asList(application, "processes", variables, raw -> getProcess((Map<String, Object>) raw, variables), builder::processe);
asList(application, "services", variables, raw -> getService(raw, variables), builder::service);
asList(application, "sidecars", variables, raw -> getSidecar((Map<String, Object>) raw, variables), builder::sidecar);
as(application, "labels", variables, Map.class::cast, builder::labels);
as(application, "annotations", variables, Map.class::cast, builder::annotations);
as(application, "metadata", variables, raw -> getMetadata((Map<String, Object>)raw, variables), builder::metadata);
asBoolean(application, "default-route", variables, builder::defaultRoute);

return builder;
}

private static ManifestV3Metadata getMetadata(Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Metadata.Builder builder = ManifestV3Metadata.builder();
asMapOfStringString(raw, "labels", variables, builder::label);
asMapOfStringString(raw, "annotations", variables, builder::annotation);
return builder.build();
}

private static ManifestV3Sidecar getSidecar(Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Sidecar.Builder builder = ManifestV3Sidecar.builder();

Expand Down Expand Up @@ -210,8 +216,8 @@ private static Map<String, Object> toApplicationYaml(ManifestV3Application appli
putIfPresent(yaml, "default-route", application.getDefaultRoute());
putIfPresent(yaml, "services", convertCollection(application.getServices(), ApplicationManifestUtilsV3::toServiceYaml));
putIfPresent(yaml, "sidecars", convertCollection(application.getSidecars(), ApplicationManifestUtilsV3::toSidecarsYaml));
putIfPresent(yaml, "labels", application.getLabels());
putIfPresent(yaml, "annotations", application.getAnnotations());
putIfPresent(yaml, "metadata", application, ApplicationManifestUtilsV3::toMetadataYamlBackwardsCompatible);
putIfPresent(yaml, "metadata", application.getMetadata(), ApplicationManifestUtilsV3::toMetadataYaml);
return yaml;
}

Expand Down Expand Up @@ -248,4 +254,24 @@ private static Map<String, Object> toProcessYaml(ManifestV3Process process) {
putIfPresent(yaml, "timeout", process.getTimeout());
return yaml;
}

private static Map<String, Object> toMetadataYaml(ManifestV3Metadata metadata) {
if (metadata == null) return null;
Map<String, Object> yaml = new TreeMap<>();
putIfPresent(yaml, "annotations", metadata.getAnnotations());
putIfPresent(yaml, "labels", metadata.getLabels());
return yaml;
}

@SuppressWarnings("deprecation")
private static Map<String, Object> toMetadataYamlBackwardsCompatible(ManifestV3Application application) {
Map<String, String> labels = application.getLabels();
Map<String, String> annotations = application.getAnnotations();
if (labels == null && annotations == null) return null;
ManifestV3Metadata metadata = ManifestV3Metadata.builder()
.labels(labels)
.annotations(annotations)
.build();
return toMetadataYaml(metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,16 @@
abstract class _ManifestV3Application extends _ApplicationManifestCommon {

/**
* The annotations configured for this application
* The metadata configured for this application. May contain labels and annotations
*/
@AllowNulls
@Nullable
abstract Map<String, String> getAnnotations();

abstract ManifestV3Metadata getMetadata();
/**
* Generate a default route based on the application name
*/
@Nullable
abstract Boolean getDefaultRoute();

/**
* The labels configured for this application
*/
@AllowNulls
@Nullable
abstract Map<String, String> getLabels();

/**
* The collection of processes configured for this application
*/
Expand All @@ -69,6 +60,24 @@ abstract class _ManifestV3Application extends _ApplicationManifestCommon {
@Nullable
abstract List<ManifestV3Sidecar> getSidecars();

/**
* The annotations configured for this application
* @deprecated use metadata().getAnnotations() instead
*/
@AllowNulls
@Nullable
@Deprecated
abstract Map<String, String> getAnnotations();

/**
* The labels configured for this application
@deprecated use metadata().getLabels() instead
*/
@AllowNulls
@Nullable
@Deprecated
abstract Map<String, String> getLabels();

public abstract static class Builder implements _ApplicationManifestCommon.Builder {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013-2021 the original author or 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 org.cloudfoundry.operations.applications;


import org.cloudfoundry.AllowNulls;
import org.cloudfoundry.Nullable;
import org.immutables.value.Value;

import java.util.Map;


@Value.Immutable
abstract class _ManifestV3Metadata {

@AllowNulls
@Nullable
abstract Map<String, String> getAnnotations();

@AllowNulls
@Nullable
abstract Map<String, String> getLabels();
}
Loading