@Override
    public ObjectNode encode(K8sApiConfig entity, CodecContext context) {
        ObjectNode node = context.mapper().createObjectNode()
                .put(CLUSTER_NAME, entity.clusterName())
                .put(SEGMENT_ID, entity.segmentId())
                .put(MODE, entity.mode().name())
                .put(SCHEME, entity.scheme().name())
                .put(IP_ADDRESS, entity.ipAddress().toString())
                .put(PORT, entity.port())
                .put(STATE, entity.state().name())
                .put(DVR, entity.dvr());

        if (entity.scheme() == HTTPS) {
            node.put(CA_CERT_DATA, entity.caCertData())
                .put(CLIENT_CERT_DATA, entity.clientCertData())
                .put(CLIENT_KEY_DATA, entity.clientKeyData());

            if (entity.token() != null) {
                node.put(TOKEN, entity.token());
            }

        } else {
            if (entity.token() != null) {
                node.put(TOKEN, entity.token());
            }

            if (entity.caCertData() != null) {
                node.put(CA_CERT_DATA, entity.caCertData());
            }

            if (entity.clientCertData() != null) {
                node.put(CLIENT_CERT_DATA, entity.clientCertData());
            }

            if (entity.clientKeyData() != null) {
                node.put(CLIENT_KEY_DATA, entity.clientKeyData());
            }
        }

        if (entity.extNetworkCidr() != null) {
            node.put(EXT_NETWORK_CIDR, entity.extNetworkCidr().toString());
        }

        ArrayNode infos = context.mapper().createArrayNode();
        entity.infos().forEach(info -> {
            ObjectNode infoJson = context.codec(HostNodesInfo.class).encode(info, context);
            infos.add(infoJson);
        });
        node.set(HOST_NODES_INFO, infos);

        return node;
    }
@Test
    public void testK8sApiConfigEncode() {
        HostNodesInfo info = new DefaultHostNodesInfo.Builder()
                .hostIp(IpAddress.valueOf("192.168.10.10"))
                .nodes(ImmutableSet.of("master", "worker"))
                .build();

        K8sApiConfig config = DefaultK8sApiConfig.builder()
                .clusterName("kubernetes")
                .segmentId(1)
                .extNetworkCidr(IpPrefix.valueOf("192.168.200.0/24"))
                .mode(K8sApiConfig.Mode.NORMAL)
                .scheme(K8sApiConfig.Scheme.HTTPS)
                .ipAddress(IpAddress.valueOf("10.10.10.23"))
                .port(6443)
                .state(CONNECTED)
                .token("token")
                .caCertData("caCertData")
                .clientCertData("clientCertData")
                .clientKeyData("clientKeyData")
                .infos(ImmutableSet.of(info))
                .dvr(true)
                .build();

        ObjectNode configJson = k8sApiConfigCodec.encode(config, context);
        assertThat(configJson, matchesK8sApiConfig(config));
    }