com.netflix.spinnaker.clouddriver.kubernetes.provider.KubernetesModelUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.netflix.spinnaker.clouddriver.kubernetes.provider.KubernetesModelUtil.java

Source

/*
 * Copyright 2017 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.provider;

import com.netflix.spinnaker.clouddriver.model.HealthState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class KubernetesModelUtil {
    public static long translateTime(String time) {
        try {
            return StringUtils.isNotEmpty(time)
                    ? (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(time)).getTime()
                    : 0;
        } catch (ParseException e) {
            log.error("Failed to parse kubernetes timestamp", e);
            return 0;
        }
    }

    public static HealthState getHealthState(List<Map<String, String>> health) {
        return someUpRemainingUnknown(health) ? HealthState.Up
                : someSucceededRemainingUnknown(health) ? HealthState.Succeeded
                        : anyStarting(health) ? HealthState.Starting
                                : anyDown(health) ? HealthState.Down
                                        : anyFailed(health) ? HealthState.Failed
                                                : anyOutOfService(health) ? HealthState.OutOfService
                                                        : HealthState.Unknown;
    }

    private static boolean stateEquals(Map<String, String> health, HealthState state) {
        String healthState = health.get("state");
        return healthState != null && healthState.equals(state.name());
    }

    private static boolean someUpRemainingUnknown(List<Map<String, String>> healthsList) {
        List<Map<String, String>> knownHealthList = healthsList.stream()
                .filter(h -> !stateEquals(h, HealthState.Unknown)).collect(Collectors.toList());

        return !knownHealthList.isEmpty() && knownHealthList.stream().allMatch(h -> stateEquals(h, HealthState.Up));
    }

    private static boolean someSucceededRemainingUnknown(List<Map<String, String>> healthsList) {
        List<Map<String, String>> knownHealthList = healthsList.stream()
                .filter(h -> !stateEquals(h, HealthState.Unknown)).collect(Collectors.toList());

        return !knownHealthList.isEmpty()
                && knownHealthList.stream().allMatch(h -> stateEquals(h, HealthState.Succeeded));
    }

    private static boolean anyDown(List<Map<String, String>> healthsList) {
        return healthsList.stream().anyMatch(h -> stateEquals(h, HealthState.Down));
    }

    private static boolean anyStarting(List<Map<String, String>> healthsList) {
        return healthsList.stream().anyMatch(h -> stateEquals(h, HealthState.Starting));
    }

    private static boolean anyFailed(List<Map<String, String>> healthsList) {
        return healthsList.stream().anyMatch(h -> stateEquals(h, HealthState.Failed));
    }

    private static boolean anyOutOfService(List<Map<String, String>> healthsList) {
        return healthsList.stream().anyMatch(h -> stateEquals(h, HealthState.OutOfService));
    }
}