-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kubernetes): Add raw resources endpoint (#5057)
* feat(kubernetes): Add raw resources endpoint This allows a Spinnaker user to query every kind of resource for an application, so you could have a nice view (which we're working on) that shows: secrets, hpas, config maps, webhooks, roles, pvcs, etc The raw resource only has a provider for Kubernetes now but it's a generic format so it could very well support other providers * Moved entire raw resource implementation to the Kubernetes provider * Add rawResource provider + cacheAllRelationships tests * Remove cloudProvider attribute
- Loading branch information
1 parent
c1b8bbe
commit bd35114
Showing
11 changed files
with
457 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...om/netflix/spinnaker/clouddriver/kubernetes/caching/view/model/KubernetesRawResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2020 Coveo, Inc. | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.caching.view.model; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.netflix.spinnaker.cats.cache.CacheData; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.Keys; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.agent.KubernetesCacheDataConverter; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesApiVersion; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesKind; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesManifest; | ||
import com.netflix.spinnaker.moniker.Moniker; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
import lombok.Value; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Value | ||
public final class KubernetesRawResource implements KubernetesResource { | ||
private static final Logger log = LoggerFactory.getLogger(KubernetesRawResource.class); | ||
private final String account; | ||
private final String name; | ||
private final String namespace; | ||
private final String displayName; | ||
private final KubernetesApiVersion apiVersion; | ||
private final KubernetesKind kind; | ||
private final Map<String, String> labels; | ||
private final Moniker moniker; | ||
private final Long createdTime; | ||
|
||
private KubernetesRawResource(KubernetesManifest manifest, String key, Moniker moniker) { | ||
this.account = ((Keys.InfrastructureCacheKey) Keys.parseKey(key).get()).getAccount(); | ||
this.name = manifest.getFullResourceName(); | ||
this.displayName = manifest.getName(); | ||
this.apiVersion = manifest.getApiVersion(); | ||
this.kind = manifest.getKind(); | ||
this.namespace = manifest.getNamespace(); | ||
this.labels = ImmutableMap.copyOf(manifest.getLabels()); | ||
this.moniker = moniker; | ||
this.createdTime = manifest.getCreationTimestampEpochMillis(); | ||
} | ||
|
||
@Nullable | ||
@ParametersAreNonnullByDefault | ||
public static KubernetesRawResource fromCacheData(CacheData cd) { | ||
KubernetesManifest manifest = KubernetesCacheDataConverter.getManifest(cd); | ||
if (manifest == null) { | ||
log.warn("Cache data {} inserted without a manifest", cd.getId()); | ||
return null; | ||
} | ||
Moniker moniker = KubernetesCacheDataConverter.getMoniker(cd); | ||
return new KubernetesRawResource(manifest, cd.getId(), moniker); | ||
} | ||
|
||
public String getRegion() { | ||
return namespace; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...spinnaker/clouddriver/kubernetes/caching/view/provider/KubernetesRawResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2020 Coveo, Inc. | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.caching.view.provider; | ||
|
||
import static com.netflix.spinnaker.clouddriver.kubernetes.caching.Keys.LogicalKind.APPLICATIONS; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.netflix.spinnaker.cats.cache.CacheData; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.Keys.ApplicationCacheKey; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.view.model.KubernetesRawResource; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.config.KubernetesConfigurationProperties; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.config.RawResourcesEndpointConfig; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class KubernetesRawResourceProvider { | ||
private final KubernetesCacheUtils cacheUtils; | ||
private final RawResourcesEndpointConfig configuration; | ||
|
||
@Autowired | ||
KubernetesRawResourceProvider( | ||
KubernetesCacheUtils cacheUtils, KubernetesConfigurationProperties globalConfig) { | ||
this.cacheUtils = cacheUtils; | ||
this.configuration = globalConfig.getRawResourcesEndpointConfig(); | ||
this.configuration.validate(); | ||
} | ||
|
||
public Set<KubernetesRawResource> getApplicationRawResources(String application) { | ||
return cacheUtils | ||
.getSingleEntry(APPLICATIONS.toString(), ApplicationCacheKey.createKey(application)) | ||
.map( | ||
applicationData -> | ||
fromRawResourceCacheData(cacheUtils.getAllRelationships(applicationData))) | ||
.orElseGet(ImmutableSet::of); | ||
} | ||
|
||
private Set<KubernetesRawResource> fromRawResourceCacheData( | ||
Collection<CacheData> rawResourceData) { | ||
Set<String> kinds = configuration.getKinds(); | ||
Set<String> omitKinds = configuration.getOmitKinds(); | ||
return rawResourceData.stream() | ||
.map(KubernetesRawResource::fromCacheData) | ||
.filter(Objects::nonNull) | ||
.filter( | ||
resource -> | ||
(kinds.isEmpty() || kinds.contains(resource.getKind().toString())) | ||
&& !omitKinds.contains(resource.getKind().toString())) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
.../java/com/netflix/spinnaker/clouddriver/kubernetes/config/RawResourcesEndpointConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2020 Coveo, Inc. | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.config; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class RawResourcesEndpointConfig { | ||
private Set<String> kinds = new HashSet<>(); | ||
private Set<String> omitKinds = new HashSet<>(); | ||
|
||
public void validate() { | ||
if (!omitKinds.isEmpty() && !kinds.isEmpty()) { | ||
throw new IllegalArgumentException("At most one of 'kinds' and 'omitKinds' can be specified"); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../java/com/netflix/spinnaker/clouddriver/kubernetes/controllers/RawResourceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.controllers; | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.view.model.KubernetesRawResource; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.view.provider.KubernetesRawResourceProvider; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.prepost.PostAuthorize; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) | ||
class RawResourceController { | ||
private final KubernetesRawResourceProvider rawResourceProvider; | ||
|
||
@Autowired | ||
public RawResourceController(KubernetesRawResourceProvider rawResourceProvider) { | ||
this.rawResourceProvider = rawResourceProvider; | ||
} | ||
|
||
@PreAuthorize("hasPermission(#application, 'APPLICATION', 'READ')") | ||
@PostAuthorize("@authorizationSupport.filterForAccounts(returnObject)") | ||
@RequestMapping(value = "/applications/{application}/rawResources", method = RequestMethod.GET) | ||
List<KubernetesRawResource> list(@PathVariable String application) { | ||
return new ArrayList<>(rawResourceProvider.getApplicationRawResources(application)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.