Skip to content

Commit

Permalink
feat(provider/kubernetes): v2 cache & surface events per-resource (#2464
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lwander committed Mar 29, 2018
1 parent 63b4599 commit 93bb22d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2018 Google, 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.v2.caching.agent;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.cats.agent.AgentDataType;
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiVersion;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials;
import io.kubernetes.client.models.V1Event;
import io.kubernetes.client.models.V1ObjectReference;
import lombok.Getter;
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE;

public class KubernetesEventCachingAgent extends KubernetesV2CachingAgent {
KubernetesEventCachingAgent(KubernetesNamedAccountCredentials<KubernetesV2Credentials> namedAccountCredentials,
ObjectMapper objectMapper,
Registry registry,
int agentIndex,
int agentCount) {
super(namedAccountCredentials, objectMapper, registry, agentIndex, agentCount);
}

@Getter
final private Collection<AgentDataType> providedDataTypes = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(
AUTHORITATIVE.forType(KubernetesKind.EVENT.toString())
))
);

@Override
protected boolean hasClusterRelationship() {
return false;
}

@Override
protected KubernetesKind primaryKind() {
return KubernetesKind.EVENT;
}

@Override
protected Map<KubernetesManifest, List<KubernetesManifest>> loadSecondaryResourceRelationships(List<KubernetesManifest> primaryResourceList) {
Map<KubernetesManifest, List<KubernetesManifest>> result = primaryResourceList.stream()
.map(m -> ImmutablePair.of(m, KubernetesCacheDataConverter.getResource(m, V1Event.class)))
.collect(Collectors.toMap(ImmutablePair::getLeft, p -> Collections.singletonList(involvedManifest(p.getRight()))));

return result;
}

private KubernetesManifest involvedManifest(V1Event event) {
V1ObjectReference ref = event.getInvolvedObject();
KubernetesManifest result = new KubernetesManifest();
result.put("metadata", new HashMap<String, Object>());

result.setApiVersion(KubernetesApiVersion.fromString(ref.getApiVersion()));
result.setKind(KubernetesKind.fromString(ref.getKind()));
result.setNamespace(ref.getNamespace());
result.setName(ref.getName());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
Expand All @@ -40,4 +42,5 @@ public class KubernetesV2Manifest implements Manifest {
private KubernetesManifest manifest;
private Status status;
private Set<Artifact> artifacts = new HashSet<>();
private List<KubernetesManifest> events = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class KubernetesV2ManifestProvider implements ManifestProvider<KubernetesV2Manifest> {
Expand Down Expand Up @@ -73,6 +78,14 @@ public KubernetesV2Manifest getManifest(String account, String location, String
return null;
}

Function<KubernetesManifest, String> lastEventTimestamp = (m) -> (String) m.getOrDefault("lastTimestamp", m.getOrDefault("firstTimestamp", "n/a"));

List<KubernetesManifest> events = cacheUtils.getTransitiveRelationship(kind.toString(), Collections.singletonList(key), KubernetesKind.EVENT.toString())
.stream()
.map(KubernetesCacheDataConverter::getManifest)
.sorted(Comparator.comparing(lastEventTimestamp))
.collect(Collectors.toList());

KubernetesHandler handler = properties.getHandler();

KubernetesManifest manifest = KubernetesCacheDataConverter.getManifest(data);
Expand All @@ -85,6 +98,7 @@ public KubernetesV2Manifest getManifest(String account, String location, String
.moniker(moniker)
.status(handler.status(manifest))
.artifacts(handler.listArtifacts(manifest))
.events(events)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class KubernetesKind {
public static KubernetesKind CONTROLLER_REVISION = new KubernetesKind("controllerRevision");
public static KubernetesKind DAEMON_SET = new KubernetesKind("daemonSet", "ds");
public static KubernetesKind DEPLOYMENT = new KubernetesKind("deployment", "deploy");
public static KubernetesKind EVENT = new KubernetesKind("event");
public static KubernetesKind HORIZONTAL_POD_AUTOSCALER = new KubernetesKind("horizontalpodautoscaler", "hpa");
public static KubernetesKind INGRESS = new KubernetesKind("ingress", "ing");
public static KubernetesKind JOB = new KubernetesKind("job");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 Google, 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.v2.op.handler;

import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent.KubernetesEventCachingAgent;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent.KubernetesV2CachingAgent;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesSpinnakerKindMap;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.model.Manifest;
import org.springframework.stereotype.Component;

@Component
public class KubernetesEventHandler extends KubernetesHandler {
@Override
public KubernetesKind kind() {
return KubernetesKind.EVENT;
}

@Override
public boolean versioned() {
return false;
}

@Override
public KubernetesSpinnakerKindMap.SpinnakerKind spinnakerKind() {
return KubernetesSpinnakerKindMap.SpinnakerKind.UNCLASSIFIED;
}

@Override
public Manifest.Status status(KubernetesManifest manifest) {
return new Manifest.Status();
}

@Override
public Class<? extends KubernetesV2CachingAgent> cachingAgentClass() {
return KubernetesEventCachingAgent.class;
}
}

0 comments on commit 93bb22d

Please sign in to comment.